keep only alphabet characters

后端 未结 5 1930
离开以前
离开以前 2021-02-06 22:34

What method should I follow in java to produce

\"WordWord\"

from

\"Word#$#$% Word 1234\"
相关标签:
5条回答
  • 2021-02-06 23:18

    You can use Character.isLetter(char c) in Character class like this

     String s = "Word#$#$% Word 1234";
     StringBuffer r = new StringBuffer();
     for (int k = 0; k < s.length(); k++) {
         if(Character.isLetter(s.charAt(k)))
            r.append(s.charAt(k));     
     }
     System.out.println("Result " + r.toString());
    
    0 讨论(0)
  • 2021-02-06 23:28

    The following answer is in more readable form

     String inputString = "Word#$#$% Word 1234";
            StringBuffer stringBuffer = new StringBuffer();
            for (int k = 0; k < inputString.length(); k++) {
    
    
                if (Character.isSpaceChar(inputString.charAt(k))) {
                    stringBuffer.append(" ");
                } else {
    
                    if (Character.isLetter(inputString.charAt(k)))
                        stringBuffer.append(inputString.charAt(k));
                }
            }
            System.out.println("Output : " + stringBuffer.toString());
        }
    

    Output : Word Word

    0 讨论(0)
  • 2021-02-06 23:29
    public static void main(String[] args) {
        String input = "Word#$#$% Word 1234";
        String extract = input.replaceAll("[^a-zA-Z]+", "");
        System.out.println(extract);
    }
    

    output:

    WordWord
    
    0 讨论(0)
  • 2021-02-06 23:29

    Using a stream:

    List<Character> letters = string.chars()
        .mapToObj(i -> (char) i)
        .filter(Character::isAlphabetic)
        .collect(Collectors.toList());
    
    0 讨论(0)
  • 2021-02-06 23:30

    You can use String.replaceAll(regex, replacement) with the regex [^A-Za-z]+ like this:

    String newstr = "Word#$#$% Word 1234".replaceAll("[^A-Za-z]+", "");
    // newstr will become WordWord
    

    Edit: Although OP hasn't mentioned anything about unicode characters but since @Joey has made a comment and if at all there a requirement to keep unicode characters then \\P{L}+ regex should be used like this:

    String newstr = "Word#$#$% Word λ1234ä, ñ, ж".replaceAll("\\P{L}+", "");
    // newstr will become WordWordλäñж
    
    0 讨论(0)
提交回复
热议问题