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

前端 未结 12 1936
爱一瞬间的悲伤
爱一瞬间的悲伤 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:29

    As of 2011 you can use Apache Commons StringUtils.stripAccents(input) (since 3.0):

        String input = StringUtils.stripAccents("Tĥïŝ ĩš â fůňķŷ Šťŕĭńġ");
        System.out.println(input);
        // Prints "This is a funky String"
    

    Note:

    The accepted answer (Erick Robertson's) doesn't work for Ø or Ł. Apache Commons 3.5 doesn't work for Ø either, but it does work for Ł. After reading the Wikipedia article for Ø, I'm not sure it should be replaced with "O": it's a separate letter in Norwegian and Danish, alphabetized after "z". It's a good example of the limitations of the "strip accents" approach.

提交回复
热议问题