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;
}
One line:
String output = input.replaceAll("\\b\\w{1,4}\\b\\s?", "");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With