Java : Normalizer 示例
Normalizer (Java SE 22 & JDK 22) 示例。
您将在大多数 Normalizer 方法中找到代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
final var values = Normalizer.Form.values();
System.out.println(Arrays.toString(values)); // [NFD, NFC, NFKD, NFKC]
final var sources = List.of("Å", "¼", "⑩", "㌀");
for (final var src : sources) {
System.out.println("----------");
System.out.println("src : " + src);
for (final var form : Normalizer.Form.values()) {
if (!Normalizer.isNormalized(src, form)) {
System.out.println(" " + form + " : " + Normalizer.normalize(src, form));
}
}
}
// Result
// ↓
//----------
//src : Å
// NFD : Å
// NFC : Å
// NFKD : Å
// NFKC : Å
//----------
//src : ¼
// NFKD : 1⁄4
// NFKC : 1⁄4
//----------
//src : ⑩
// NFKD : 10
// NFKC : 10
//----------
//src : ㌀
// NFKD : アパート
// NFKC : アパート
Methods
static boolean isNormalized (CharSequence src, Normalizer.Form form)
final var src = "⑩";
final var form = Normalizer.Form.NFKC;
System.out.println(Normalizer.isNormalized(src, form)); // false
final var normalized = Normalizer.normalize(src, form);
System.out.println(normalized); // 10
System.out.println(Normalizer.isNormalized(normalized, form)); // true
static String normalize (CharSequence src, Normalizer.Form form)
final var src = "⑩";
final var form = Normalizer.Form.NFKC;
System.out.println(Normalizer.isNormalized(src, form)); // false
final var normalized = Normalizer.normalize(src, form);
System.out.println(normalized); // 10
System.out.println(Normalizer.isNormalized(normalized, form)); // true