輸入字符串:從字符串中刪除短字和字符的Java
String input = "Lorem Ipsum is simply dummy text of the printing and typesetting industry";
輸出字符串:
String output = "Lorem Ipsum simply dummy printing typesetting industry";
的是換掉短詞的最好方法?
這裏我的第一個想法:
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;
}
定義 '短'。是否刪除少於5個字符的任何單詞? – PakkuDon
我推薦使用'StringBuilder'。 – ChiefTwoPencils