How can I remove punctuation from input text in Java?

后端 未结 5 1178

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:25

    This first removes all non-letter characters, folds to lowercase, then splits the input, doing all the work in a single line:

    String[] words = instring.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+");
    

    Spaces are initially left in the input so the split will still work.

    By removing the rubbish characters before splitting, you avoid having to loop through the elements.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-02 07:38

    You can use following regular expression construct

    Punctuation: One of !"#$%&'()*+,-./:;<=>?@[]^_`{|}~

    inputString.replaceAll("\\p{Punct}", "");
    
    0 讨论(0)
  • 2020-12-02 07:41

    If you don't want to use RegEx (which seems highly unnecessary given your problem), perhaps you should try something like this:

    public String modified(final String input){
        final StringBuilder builder = new StringBuilder();
        for(final char c : input.toCharArray())
            if(Character.isLetterOrDigit(c))
                builder.append(Character.isLowerCase(c) ? c : Character.toLowerCase(c));
        return builder.toString();
    }
    

    It loops through the underlying char[] in the String and only appends the char if it is a letter or digit (filtering out all symbols, which I am assuming is what you are trying to accomplish) and then appends the lower case version of the char.

    0 讨论(0)
  • 2020-12-02 07:43

    You may try this:-

    Scanner scan = new Scanner(System.in);
    System.out.println("Type a sentence and press enter.");
    String input = scan.nextLine();
    String strippedInput = input.replaceAll("\\W", "");
    System.out.println("Your string: " + strippedInput);
    

    [^\w] matches a non-word character, so the above regular expression will match and remove all non-word characters.

    0 讨论(0)
提交回复
热议问题