Java从字符串中选择单词

嗨,大家好。 对于这个令人尴尬的新手问题,我很抱歉,但我似乎无法弄明白这个命令。 我对python很好,并且在jython中有一个脚本,我正在转向纯java(并沿途学习)。

我有一个字符串: Java is really cool

我知道如何剥离字符串以获得最终结果: really cool

但我不确定在java中执行它的命令。 我发现java中的命令是专门通过文本来完成的,但我想使用空格作为分隔符来获取单词。

有人能告诉我使用什么java命令吗? 我希望能够删除前两个单词和/或专门选择我想要的单词。

谢谢,

我想你正在寻找String.split

 String s = "Java is really cool"; String words[] = s.split(" "); String firstTwo = words[0] + " " + words[1]; // first two words String lastTwo = words[words.length - 2] + " " + words[words.length - 1]; // last two words 

请看一下String.split方法

 String foo = "java is really cool"; String bar[] = foo.split(" "); 

这会将所有单词分成一个数组。