简体   繁体   English

Java从字符串中选择单词

[英]Java selecting words from a string

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). 我对python很好,并且在jython中有一个脚本,我正在转向纯java(并沿途学习)。

I have a string: Java is really cool 我有一个字符串: Java is really cool

I know how to strip the string to get the final result: really cool 我知道如何剥离字符串以获得最终结果: really cool

but i'm not sure the command to do it in java. 但我不确定在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. 我发现java中的命令是专门通过文本来完成的,但我想使用空格作为分隔符来获取单词。

Can someone tell me what java command to use? 有人能告诉我使用什么java命令吗? I would want to be able either remove the first two words and/or specifically select the words I want. 我希望能够删除前两个单词和/或专门选择我想要的单词。

Thanks, 谢谢,

I think you are looking for String.split . 我想你正在寻找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(" ");

this will separate all of the words into an array. 这会将所有单词分成一个数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM