keep only alphabet characters

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

What method should I follow in java to produce

\"WordWord\"

from

\"Word#$#$% Word 1234\"
5条回答
  •  不思量自难忘°
    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

提交回复
热议问题