虽然它可能是诱人抛出一堆维权的和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
我必须得到一个字符串中每个单词的长度。 –