例如-
text = Československá obchodní banka;
text string contains diacritics like Č , á etc.
我想编写一个函数,我将在其中传递此字符串“Československá obchodní banka”,如果字符串包含变音符号,函数将返回 true,否则返回 false
。
我必须分别处理变音符号和包含不在 A-z 或 a-z 范围内的字符的字符串。
1) If String contains diacritics then I have to do some XXXXXX on it.
2) If String contains character other than A-Z or a-z and not contains diacritics then do some other operations YYYYY.
我不知道该怎么做。
最佳答案
一条知识:在 Unicode 中存在 á
的代码,但使用 a
和 组合标记-'< 可能会得到相同的结果
.
您可以使用 java.text.Normalizer ,如下:
public static boolean hasDiacritics(String s) {
// Decompose any á into a and combining-'.
String s2 = Normalizer.normalize(s, Normalizer.Form.NFD);
return s2.matches("(?s).*\\p{InCombiningDiacriticalMarks}.*");
//return !s2.equals(s);
}
关于java - 我怎么知道我的字符串包含变音符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11309324/