Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove short words and characters from a string Java

Tags:

java

string

regex

Input string:

String input = "Lorem Ipsum is simply dummy text of the printing and typesetting industry";

Output string:

String output = "Lorem Ipsum simply dummy printing typesetting industry";

What is the best way to remove short words?

Here my first idea:

private String removeShortWords(String string){
    int minLength = 5;
    String result = "";

    String[] words = string.split("\\s+");

    for (int i = 0; i < words.length; i++){
        String word = words[i];
        if(word.length() >= minLength){
            result += word + " ";
        }
    }       

    return result;
}
like image 655
TUNER88 Avatar asked Apr 25 '26 22:04

TUNER88


1 Answers

One line:

String output = input.replaceAll("\\b\\w{1,4}\\b\\s?", "");
like image 82
Boann Avatar answered Apr 27 '26 10:04

Boann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!