Java word selection from a string

Hello to all. I apologize for this awkward question by the newbies, but I can't figure out how to do this. I am fine with python and had a script in jython that I convert to pure java (and learning along the way).

I have a line: Java is really cool

I know how to delete a row to get the final result: really cool

but i'm not sure if this is a command in java. I found commands in java to do this specifically by text, but I want to use the space as a separator and get the words.

Can someone tell me what to use java command? I would like to be able to delete the first two words and / or specifically select the words that I want.

Thanks,

+4
source share
3 answers

I think you are looking for 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 
+8
source

Please take a look at the String.split method

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

this will split all the words into an array.

+2
source

All Articles