Di seguito ho fornito alcuni esempi e ho pensato che il latino fosse abbastanza, ma ...
C'è un modo per rimuovere tutti questi segni dalla stringa di input e mantenere solo le lettere e la punteggiatura nelle diverse lingue?
Dopo la modifica, ho sviluppato una nuova soluzione, usando il Character.getType
metodo, e sembra essere la soluzione migliore.
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);
}
}
}
Produzione:
---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
早上好 ♛
早上好
Καλημέρα ✂
Καλημέρα
Il codice funziona eseguendo lo streaming della stringa in punti di codice. Quindi usando lambdas per filtrare i caratteri in aint
array, quindi convertiamo l'array in String.
Le lettere e gli spazi utilizzano i metodi Carattere per filtrare, non bene con la punteggiatura. Tentativo fallito .
L' unicode blocca il filtro bianco utilizzando i blocchi unicode che il programmatore specifica come consentito. Tentativo fallito .
L' unicode blocca il filtro nero usando i blocchi unicode che il programmatore specifica come non consentito. Tentativo fallito .
Il filtro di categoria usando il metodo statico Character.getType
. Il programmatore può definire category
nell'array quali tipi sono consentiti. LAVORI 😨😱😰😲😀.