Java 从字符串中提取每个单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19144244/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:29:10  来源:igfitidea点击:

Extracting each word from a string

javastring

提问by tjg92

I'm trying to count the number of words in a String, find the length of each word in a String and then determine the largest word in the String using only the String class. I can't use arrays. Does anyone know a way to extract each word from the string?

我正在尝试计算字符串中的单词数,找到字符串中每个单词的长度,然后仅使用 String 类确定字符串中最大的单词。我不能使用数组。有谁知道从字符串中提取每个单词的方法?

采纳答案by Little Child

int indexOfSpace = 0;
int nextIndexOfSpace = 0;

String sentence  = "This is a sentence";

int lastIndexOfSpace = sentence.lastIndexOf(" ");
while(indexOfSpace != lastIndexOfSpace){
    nextIndexOfSpace = sentence.indexOf(" ",indexOfSpace);  
    String word = sentence.subString(indexOfSpace,nextIndexOfSpace);
    System.out.println("Word: " + word + " Length: " + word.length());
    indexOfSpace = nextIndexOfSpace;
}

String lastWord = sentence.subString(lastIndexOfSpace);
System.out.println("Word: " + lastWord + " Length: " + lastWord.length());

You need to do something along the above lines. Since your question seems like a homework question, I am not going to put an effort into debugging this. This is as far as I can go into answering what seems like a homework question.

您需要按照上述方式做一些事情。由于您的问题似乎是一个家庭作业问题,因此我不会努力调试它。这就是我可以回答的似乎是家庭作业问题的范围。

Debug it, use it.

调试它,使用它。

回答by vandale

Scanner s= new Scanner("Put your string here");
while(s.hasNext()){
    String word= s.next();
}

Edit using only String:

仅使用字符串进行编辑:

String myString = "hello world how are you";
for (int i = 0,                   //start of word
     j = 0;                       //end of word
     i < myString.length();       //make sure we're in bounds
     i = j + 1) {                 //Start from where we left off plus one
                                  //to get rid of space we just found

    j = myString.indexOf(" ", i); //find the next space
    if (j == -1) {                //-1 means no more spaces so we're done
        break;
    }
    String word = myString.substring(i, j); //here is your word
}

回答by Ankit Rustagi

Use StringTokenizer

使用StringTokenizer

String sentence = "This is a sentence"; 
StringTokenizer t = new StringTokenizer(sentence);
String word ="";
while(t.hasMoreTokens())
{
    word = t.nextToken();
    System.out.println(word);
}

The Output should be

输出应该是

This
is
a
sentence

回答by Kaushik Sivakumar

This can be done using the split(" ")//which splits the string(maybe a sentence) by spaces into words and use array list to store List words = Arrays.asList(sentence.split(" "));

这可以使用 split(" ")// 将字符串(可能是一个句子)按空格拆分成单词并使用数组列表来存储 List words = Arrays.asList(sentence.split(" "));

回答by Lal

A raw version, probably efficient, could be something like this. This assumes that there are single spaces between the words including in the end which can easily be tweaked make it perfect.

一个原始版本,可能是高效的,可能是这样的。这假设单词之间有单个空格,包括结尾,可以轻松调整使其完美。



    int wordCount = 0;
    int maxWordLen = 0;
    String longestWord = null;

    if(input != null){//given word
        int currentWordStart = 0;
        for(int i = 0; i < input.length(); i++){
            char currentChar = input.charAt(i);
            if(' ' == currentChar){
                wordCount++;
                String currentWord = input.substring(currentWordStart, i);
                int currentWordLen = i - currentWordStart;
                System.out.println("Word: " + currentWord + ", Length: " + currentWordLen);
                currentWordStart = i + 1;
                if(maxWordLen < currentWordLen){
                    maxWordLen = currentWordLen;
                    longestWord = currentWord;
                }
            }
        }
    }

    System.out.println("Word count: " + wordCount);
    System.out.println("Longest word: " + longestWord + ", Length: " + maxWordLen);