Is there a way to get rid of accents and convert a whole string to regular letters?

前端 未结 12 2170
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 04:58

Is there a better way for getting rid of accents and making those letters regular apart from using String.replaceAll() method and replacing letters one by one?

12条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 05:19

    One of the best way using regex and Normalizer if you have no library is :

        public String flattenToAscii(String s) {
                    if(s == null || s.trim().length() == 0)
                            return "";
                    return Normalizer.normalize(s, Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", "");
    }
    

    This is more efficient than replaceAll("[^\p{ASCII}]", "")) and if you don't need diacritics (just like your example).

    Otherwise, you have to use the p{ASCII} pattern.

    Regards.

提交回复
热议问题