Dei alguns exemplos abaixo e pensei que o latim é suficiente, mas ...
Existe uma maneira de remover todos esses sinais da string de entrada e manter apenas as letras e a pontuação nos diferentes idiomas?
Após a edição, desenvolveu uma nova solução, usando o Character.getType
método, e essa parece ser a melhor opção.
package zmarcos.emoji;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class TestEmoji {
public static void main(String[] args) {
String[] arr = {"Remove ✅, 🔥, ✈ , ♛ and other such signs from Java string",
"→ Cats and dogs",
"I'm on 🔥",
"Apples ⚛ ",
"✅ Vi sign",
"♛ I'm the king ♛ ",
"Star me ★",
"Star ⭐ once more",
"早上好 ♛",
"Καλημέρα ✂"};
System.out.println("---only letters and spaces alike---\n");
for (String input : arr) {
int[] filtered = input.codePoints().filter((cp) -> Character.isLetter(cp) || Character.isWhitespace(cp)).toArray();
String result = new String(filtered, 0, filtered.length);
System.out.println(input);
System.out.println(result);
}
System.out.println("\n---unicode blocks white---\n");
Set<Character.UnicodeBlock> whiteList = new HashSet<>();
whiteList.add(Character.UnicodeBlock.BASIC_LATIN);
for (String input : arr) {
int[] filtered = input.codePoints().filter((cp) -> whiteList.contains(Character.UnicodeBlock.of(cp))).toArray();
String result = new String(filtered, 0, filtered.length);
System.out.println(input);
System.out.println(result);
}
System.out.println("\n---unicode blocks black---\n");
Set<Character.UnicodeBlock> blackList = new HashSet<>();
blackList.add(Character.UnicodeBlock.EMOTICONS);
blackList.add(Character.UnicodeBlock.MISCELLANEOUS_TECHNICAL);
blackList.add(Character.UnicodeBlock.MISCELLANEOUS_SYMBOLS);
blackList.add(Character.UnicodeBlock.MISCELLANEOUS_SYMBOLS_AND_ARROWS);
blackList.add(Character.UnicodeBlock.MISCELLANEOUS_SYMBOLS_AND_PICTOGRAPHS);
blackList.add(Character.UnicodeBlock.ALCHEMICAL_SYMBOLS);
blackList.add(Character.UnicodeBlock.TRANSPORT_AND_MAP_SYMBOLS);
blackList.add(Character.UnicodeBlock.GEOMETRIC_SHAPES);
blackList.add(Character.UnicodeBlock.DINGBATS);
for (String input : arr) {
int[] filtered = input.codePoints().filter((cp) -> !blackList.contains(Character.UnicodeBlock.of(cp))).toArray();
String result = new String(filtered, 0, filtered.length);
System.out.println(input);
System.out.println(result);
}
System.out.println("\n---category---\n");
int[] category = {Character.COMBINING_SPACING_MARK, Character.COMBINING_SPACING_MARK, Character.CONNECTOR_PUNCTUATION, /*Character.CONTROL,*/ Character.CURRENCY_SYMBOL,
Character.DASH_PUNCTUATION, Character.DECIMAL_DIGIT_NUMBER, Character.ENCLOSING_MARK, Character.END_PUNCTUATION, Character.FINAL_QUOTE_PUNCTUATION,
/*Character.FORMAT,*/ Character.INITIAL_QUOTE_PUNCTUATION, Character.LETTER_NUMBER, Character.LINE_SEPARATOR, Character.LOWERCASE_LETTER,
/*Character.MATH_SYMBOL,*/ Character.MODIFIER_LETTER, /*Character.MODIFIER_SYMBOL,*/ Character.NON_SPACING_MARK, Character.OTHER_LETTER, Character.OTHER_NUMBER,
Character.OTHER_PUNCTUATION, /*Character.OTHER_SYMBOL,*/ Character.PARAGRAPH_SEPARATOR, /*Character.PRIVATE_USE,*/
Character.SPACE_SEPARATOR, Character.START_PUNCTUATION, /*Character.SURROGATE,*/ Character.TITLECASE_LETTER, /*Character.UNASSIGNED,*/ Character.UPPERCASE_LETTER};
Arrays.sort(category);
for (String input : arr) {
int[] filtered = input.codePoints().filter((cp) -> Arrays.binarySearch(category, Character.getType(cp)) >= 0).toArray();
String result = new String(filtered, 0, filtered.length);
System.out.println(input);
System.out.println(result);
}
}
}
Resultado:
---only letters and spaces alike---
Remove ✅, 🔥, ✈ , ♛ and other such signs from Java string
Remove and other such signs from Java string
→ Cats and dogs
Cats and dogs
I'm on 🔥
Im on
Apples ⚛
Apples
✅ Vi sign
Vi sign
♛ I'm the king ♛
Im the king
Star me ★
Star me
Star ⭐ once more
Star once more
早上好 ♛
早上好
Καλημέρα ✂
Καλημέρα
---unicode blocks white---
Remove ✅, 🔥, ✈ , ♛ and other such signs from Java string
Remove , , , and other such signs from Java string
→ Cats and dogs
Cats and dogs
I'm on 🔥
I'm on
Apples ⚛
Apples
✅ Vi sign
Vi sign
♛ I'm the king ♛
I'm the king
Star me ★
Star me
Star ⭐ once more
Star once more
早上好 ♛
Καλημέρα ✂
---unicode blocks black---
Remove ✅, 🔥, ✈ , ♛ and other such signs from Java string
Remove , , , and other such signs from Java string
→ Cats and dogs
→ Cats and dogs
I'm on 🔥
I'm on
Apples ⚛
Apples
✅ Vi sign
Vi sign
♛ I'm the king ♛
I'm the king
Star me ★
Star me
Star ⭐ once more
Star once more
早上好 ♛
早上好
Καλημέρα ✂
Καλημέρα
---category---
Remove ✅, 🔥, ✈ , ♛ and other such signs from Java string
Remove , , , and other such signs from Java string
→ Cats and dogs
Cats and dogs
I'm on 🔥
I'm on
Apples ⚛
Apples
✅ Vi sign
Vi sign
♛ I'm the king ♛
I'm the king
Star me ★
Star me
Star ⭐ once more
Star once more
早上好 ♛
早上好
Καλημέρα ✂
Καλημέρα
O código funciona transmitindo a String para pontos de código. Em seguida, use lambdas para filtrar caracteres em umint
matriz, convertemos a matriz em String.
As letras e os espaços estão usando o método Character para filtrar, não é bom com pontuação. Falha na tentativa .
O unicode bloqueia o filtro branco usando os blocos unicode que o programador especifica conforme permitido. Falha na tentativa .
O unicode bloqueia o filtro preto usando os blocos unicode que o programador especifica como não permitido. Falha na tentativa .
O filtro de categoria usando o método estático Character.getType
. O programador pode definir na category
matriz que tipos são permitidos. TRABALHOS 😨😱😰😲😀.