雖然它可能是誘人拋出一堆維權的和IFS,這將是清潔劑只使用正則表達式:
Pattern.compile("[.,; ]+").splitAsStream(input)
完整的例子:
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class Counting {
public static void main(String... args) {
String text = "This is a string. With some punctuation, but I only care about words.";
String wordsWithLengths = Pattern.compile("[.,; ]+")
.splitAsStream(text)
.map(word -> word + " => " + word.length())
.collect(Collectors.joining("\n"));
System.out.println(wordsWithLengths);
}
}
輸出:
This => 4
is => 2
a => 1
string => 6
With => 4
some => 4
punctuation => 11
but => 3
I => 1
only => 4
care => 4
about => 5
words => 5
另外,如果你想算多少的話有N多角色越多,你可以:
import java.util.regex.Pattern;
public class CountingWords {
public static void main(String... args) {
String text = "This is a string. With some punctuation, but I only care about words.";
int threshold = 5;
long amountOfWords = Pattern.compile("[.,; ]+")
.splitAsStream(text)
.filter(word -> word.length() > threshold)
.count();
System.out.println("There are " + amountOfWords + " words with more than " + threshold + " characters");
}
}
我不明白你想幹什麼?你想刪除標點符號並獲得字符串的長度嗎? – denis
我必須得到一個字符串中每個單詞的長度。 –