How to remove diacritics only for uppercase characters in a string

江枫思渺然 提交于 2020-01-06 02:50:29

问题


I need to remove diacritics from uppercase characters in a string. Example : Électronique Caméras => Electronique Caméras (only the É is modified, é in Caméras remains as it is)

I am using the following method, which removes diacritics only from the uppercase letters, but the reconstructed string looks like this - Electronique Came?ras (é is lost). How can I reconstruct the string properly?

public static String removeDiacriticsFromUppercaseLetters(String input)
    {
        if (input == null)
              return input;
        String normalized= Normalizer.normalize(input, Normalizer.Form.NFD);

        StringBuilder newString = new StringBuilder();
        newString.append(normalized.charAt(0));

        for (int i=1;i<normalized.length();++i)
        {
            //Check if this diacritic is for an uppercase letter, if yes, skip
            if (Character.isUpperCase(normalized= .charAt(i-1)) && Character.getType(normalized.charAt(i)) == Character.NON_SPACING_MARK){
              continue;
            }
            else{
              newString.append(normalized.charAt(i));
            }
        }
        return newString.toString();
    }

Thanks

来源:https://stackoverflow.com/questions/10392339/how-to-remove-diacritics-only-for-uppercase-characters-in-a-string

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