Converting Java String to ascii

后端 未结 2 1138
暗喜
暗喜 2020-12-08 08:01

I need to convert Strings that consists of some letters specific to certain languages (like HÄSTDJUR - note Ä) to a String without those special le

2条回答
  •  感动是毒
    2020-12-08 08:45

    I think your question is the same as this one:

    Java - getting rid of accents and converting them to regular letters

    and hence the answer is also the same:

    Solution

    String convertedString = 
           Normalizer
               .normalize(input, Normalizer.Form.NFD)
               .replaceAll("[^\\p{ASCII}]", "");
    

    References

    See

    • JavaDoc: Normalizer.normalize(String, Normalizer.Form)
    • JavaDoc: Normalizer.Form.NFD
    • Sun Java Tutorial: Normalizer's API

    Example Code:

    final String input = "Tĥïŝ ĩš â fůňķŷ Šťŕĭńġ";
    System.out.println(
        Normalizer
            .normalize(input, Normalizer.Form.NFD)
            .replaceAll("[^\\p{ASCII}]", "")
    );
    

    Output:

    This is a funky String

提交回复
热议问题