在java中的特定位置拆分字符串

giu*_*lio 4 java text

假设您有一个表单的字符串"word1 word2 word3 word4".什么是分裂它使得简单的方法split[0] = "word1 word2"和split[1] = "word3 word4"?

编辑:澄清

我想拆分,而不是拆分[0] ="word1",我有前两个单词(我同意它不清楚)和所有其他单词在split [1],即在第二个空格

Ein*_*ren 6

我会使用String.substring(beginIndex,endIndex); 和String.substring(beginIndex);

String a = "word1 word2 word3 word4";
int first = a.indexOf(" ");
int second = a.indexOf(" ", first + 1);
String b = a.substring(0,second);
String c = b.subString(second); // Only startindex, cuts at the end of the string
Run Code Online (Sandbox Code Playgroud)

这将导致a ="word1 word2"和b ="word3 word4"