Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

HI Everyone. I'm sorry for this embarrassingly newbie question but I cannot seem to figure out the command to do it. I'm okay with python and had a script in jython that I'm convering to pure java(and learning along the way).

I have a string: Java is really cool

I know how to strip the string to get the final result: really cool

but i'm not sure the command to do it in java. I found commands in java to do it specifically by text but I want to use a space as a deliminator and get the words.

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

Thanks,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
617 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...