Java从字符串中选择单词 [英] Java selecting words from a string
问题描述
大家好。对于这个令人尴尬的新手问题,我很抱歉,但我似乎无法弄明白这个命令。我对python很好,并且在jython中有一个脚本,我正在转向纯java(并沿途学习)。
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).
我有一个字符串: Java真的很酷
我知道如何去除字符串以获得最终结果:真的很酷
I know how to strip the string to get the final result: really cool
但我不确定在java中执行此操作的命令。我发现java中的命令是专门通过文本来完成的,但我想使用空格作为分隔符并获取单词。
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.
有人可以告诉我使用什么java命令?我希望能够删除前两个单词和/或专门选择我想要的单词。
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.
谢谢,
推荐答案
我认为您正在寻找 String.split
。
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
这篇关于Java从字符串中选择单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!