第一轮已回答here https://stackoverflow.com/questions/20429277/java-how-can-i-split-a-string-with-multiple-spaces-on-every-nth-space/20429737?noredirect=1#20429737昨夜。 @Reimeus 给出的解决方案非常棒,并给出了以下输出:
My name is the mighty llama
To:
String[] stringArray = string.split("(?<!\\G\\S+)\\s");
My name
is the
mighty llama
然而,我现在发现自己需要纠正我正在做的事情。我实际上需要能够像这样分割字符串。
My name
name is
is the
the mighty
mighty llama
您可以将其拆分为单个单词并在代码中构建所需的数组。
String string = "My name is the mighty llama";
String[] stringArray = string.split(" ");
String[] outputArray = new String[stringArray.length - 1];
for (int i = 0; i < stringArray.length - 1; i++) {
outputArray[i] = stringArray[i] + " " + stringArray[i+1];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)