How can i get know that my String contains diacritics?

孤人 提交于 2020-01-02 02:23:11

问题


For Example -

text = Československá obchodní banka;

text string contains diacritics like Č , á etc.

I want to write a function where i will pass this string "Československá obchodní banka" and function will return true if string contains diacritics else false.

I have to handle diacritics and string which contains character which doesn't fall in A-z or a-z range separately.

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.

I have no idea how to do it.


回答1:


One piece of knowledge: in Unicode there exists a code for á but the same result one may get with an a and a combining mark-'.

You can use java.text.Normalizer, as follows:

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);
}



回答2:


The Normalizer class seems to be able to accomplish this. Some limited testing indicate that

Normalizer.isNormalized(text, Normalizer.Form.NFD)

might be what you need.



来源:https://stackoverflow.com/questions/11309324/how-can-i-get-know-that-my-string-contains-diacritics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!