我有
String input = "one two three four five six seven";
是否有一个与String.split()
一起使用的正则表达式一次抓取(最多)两个单词,这样:
String[] pairs = input.split("some regex");
System.out.println(Arrays.toString(pairs));
结果如下:
[one two,two three, three four,four five,five six,six seven]
答案 0 :(得分:5)
String[] elements = input.split(" ");
List<String> pairs = new ArrayList<>();
for (int i = 0; i < elements.length - 1; i++) {
pairs.add(elements[i] + " " + elements[i + 1]);
}
答案 1 :(得分:2)
没有。使用String.split(),你得到的东西不能重叠。
e.g。你可以得到:"one two three four"
- &gt; {"one","two","three","four"}
,但不是{"one two","two three", "three four"}