How can I remove punctuation from input text in Java?

后端 未结 5 1182

I am trying to get a sentence using input from the user in Java, and i need to make it lowercase and remove all punctuation. Here is my code:

    String[] wo         


        
5条回答
  •  无人及你
    2020-12-02 07:32

    I don't like to use regex, so here is another simple solution.

    public String removePunctuations(String s) {
        String res = "";
        for (Character c : s.toCharArray()) {
            if(Character.isLetterOrDigit(c))
                res += c;
        }
        return res;
    }
    

    Note: This will include both Letters and Digits

提交回复
热议问题