How to filter a Java String to get only alphabet characters?

后端 未结 2 907
北荒
北荒 2021-01-12 12:24

I\'m generating a XML file to make payments and I have a constraint for user\'s full names. That param only accept alphabet characters (a-ZAZ) + whitespaces to separe names

2条回答
  •  北海茫月
    2021-01-12 12:52

    You can use this removeAccents method with a later replaceAll with [^A-Za-z ]:

    public static String removeAccents(String text) {
      return text == null ? null :
        Normalizer.normalize(text, Form.NFD)
            .replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
    }
    

    The Normalizer decomposes the original characters into a combination of a base character and a diacritic sign (this could be multiple signs in different languages). á, é and í have the same sign: 0301 for marking the ' accent.

    The \p{InCombiningDiacriticalMarks}+ regular expression will match all such diacritic codes and we will replace them with an empty string.

    And in the caller:

    String original = "Carmen López-Delina Santos";
    String res = removeAccents(original).replaceAll("[^A-Za-z ]", "");
    System.out.println(res);
    

    See IDEONE demo

提交回复
热议问题