Saya memberikan beberapa contoh di bawah ini, dan berpikir bahwa bahasa Latin sudah cukup, tetapi ...
Apakah ada cara untuk menghapus semua tanda-tanda ini dari string input dan hanya menyimpan huruf & tanda baca dalam bahasa yang berbeda?
Setelah mengedit, mengembangkan solusi baru, menggunakan Character.getType
metode, dan yang tampaknya menjadi cara terbaik untuk ini.
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);
}
}
}
Keluaran:
---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
早上好 ♛
早上好
Καλημέρα ✂
Καλημέρα
Kode berfungsi dengan mengalirkan String ke titik-kode. Kemudian menggunakan lambdas untuk memfilter karakter ke dalam int
array, lalu kita mengubah array menjadi String.
The huruf dan spasi menggunakan menggunakan metode Karakter untuk filter, tidak baik dengan tanda baca. Upaya gagal .
The blok unicode putih filter menggunakan blok unicode yang menspesifikasikan programmer sebagaimana diizinkan. Upaya gagal .
The blok unicode hitam filter menggunakan blok unicode yang menspesifikasikan programmer tidak diperbolehkan. Upaya gagal .
The kategori filter menggunakan metode statis Character.getType
. Programmer dapat mendefinisikan dalam category
array tipe apa yang diperbolehkan. KARYA 😨😱😰😲😀.