在 Java 中使用 indexOf 和 substring 方法从字符串中分离单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25778086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Separating words from a string using indexOf & substring methods in Java
提问by patmarley
Yo, I'm trying to write a program in Java that separates words from a string, using only substring and indexOf. I've already made this code and it runs almost perfect but it prints alot of empty line after printing the words.
哟,我正在尝试用 Java 编写一个程序,将单词与字符串分开,仅使用子字符串和 indexOf。我已经制作了这段代码,它运行得几乎完美,但在打印单词后打印了很多空行。
class wordSep{
public static void main(String args[]){
String line = "The world is full of strangers";
String word = line.substring(0,line.indexOf(" "));
int index = line.length()-1;
while(index>=0){
System.out.println(word);
line = line.substring(line.indexOf(" ") + 1) + " ";
word = line.substring(0,line.indexOf(" "));
index--;
}
}
}
The output of this will be:
输出将是:
The
world
is
full
of
strangers
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
\empty line
I think this has to do with the condition of my while loop. I need to have an output without those empty lines.. Please help, thanks.
我认为这与我的 while 循环的条件有关。我需要一个没有那些空行的输出.. 请帮忙,谢谢。
采纳答案by Jean Logeart
Replace while(index>=0)
with while(!word.isEmpty())
and get rid of index
替换 while(index>=0)
为while(!word.isEmpty())
并去掉index
回答by Hitesh Garg
That's great that your problem is solved but it would be great if you try to use some built in functions(like String.split()) and libraries in your code as it can reduce a lot of processing and make your code more robust and independent of String length and other parameters.
您的问题得到解决真是太好了,但如果您尝试在代码中使用一些内置函数(如String.split())和库,那就太好了,因为它可以减少大量处理并使您的代码更加健壮和独立字符串长度和其他参数。
Your code could be optimized to this level...
您的代码可以优化到这个级别......
class wordSep{
public static void main(String args[]){
String line = "The world is full of strangers";
String[] word = line.split(" ");
for(String str: word){
System.out.println(str);
}
}
}
output -
输出 -
The
world
is
full
of
strangers
Now you can also use String Tokenizer But it is not recommended any more...
现在你也可以使用 String Tokenizer 但不推荐了...
Read this -> to completely understand how to split Strings in Java on some character, or using some Regex and other parameters
回答by Abhinav
J. Lucky, you can reduce the number of sop's and the size as well, like this:
J. 幸运的是,您还可以减少 sop 的数量和大小,如下所示:
class Extract
{
void Substring()
{
String s = "The World is Full of Strangers.";
s = s + " ";
int idxOfNextWord = 0;
for(int i = 0; i < s.length(); i++)
{
if(s.charAt(i)== ' ')
{
System.out.println(s.substring(idxOfNextWord, i));
idxOfNextWord = i+1;
}
}
}
}
回答by lxcky
Even though you seemed to have fixed the problem, still going to post this answer. I believe your code can be minimized into just a few lines like this:
即使您似乎已经解决了问题,但仍要发布此答案。我相信您的代码可以最小化为如下几行:
String line = "The world is full of strangers";
int idxOfNextWord = 0;
for (int i = 0; i < line.length(); i++) {
if(line.charAt(i)==' ') {
System.out.println(line.substring(idxOfNextWord, i));
idxOfNextWord = i+1;
}
}
System.out.println(line.substring(idxOfNextWord));