Traversing through a sentence word by word

倖福魔咒の 提交于 2020-05-27 14:48:31

问题


How is it possible to traverse through any given sentence word by word? Is there any in-built functions in java? I have no idea how to begin.


回答1:


Something like this:

String sentence = "Your sentence here.";
String[] words = sentence.split("\\s+"); // splits by whitespace
for (String word : words) {
    System.out.println(word);
}



回答2:


A lot of people are suggesting to split on spaces, but even this very sentence contains commas, etc. You should split on more than just spaces; split on punctuation characters too:

String words = sentence.split("([\\s.,;:\"?!,.…(){}[\\]%#/]|(- )|( -))+");

This regex splits on all reasonably expected punctuation characters. Note that the in-word hyphen and the apostrophe are not "punctuation"; they are part of the word.

This approach, or something similar, will also handle non-English character sentences.




回答3:


String[] array = input.split(" ");

That way the string is converted into an array separated by spaces (you can change the separator in the split()'s argumen) and then you can loop through the array as you want.




回答4:


Start with StringTokenizer for example or use String.split(" ")




回答5:


Try splitting the sentence by whitespace character.

String sentence = "This is a sentence.";

for(String word: sentence.split("\\s+"){
  System.out.println(word);
}



回答6:


String s="sfgasdfg  jhsadfkjashfd sajdfhjkasdfh hjskafhasj";
String wordArray[] =s.split("\\s+");
for(String sT :wordArray)
{
System.out.println(st);
}



回答7:


Take a look at the String Split function here http://www.tek-tips.com/viewthread.cfm?qid=1167964




回答8:


Assuming you already have the sentence stored as a string, you could use the String.replaceAll("[./,]"," ") method to remove the stop words and then use the String.split("\\s+") to obtain the individual words making up the phrase.




回答9:


you can use StringTokenizer class which will divide the string into words.

      public static void main(String ae[]){
    String st = "This is Java";
    StringTokenizer str= new StringTokenizer(st);
    while(str.hasMoreTokens()){
        System.out.println(str.nextToken());
    }
}



回答10:


I would Say StringTokenizer might help You.

        String str = "This is String , split by StringTokenizer, created by mkyong";
        StringTokenizer st = new StringTokenizer(str);

        System.out.println("---- Split by space ------");
        while (st.hasMoreElements()) {
            System.out.println(st.nextElement());
        }

        System.out.println("---- Split by comma ',' ------");
        StringTokenizer st2 = new StringTokenizer(str, ",");

        while (st2.hasMoreElements()) {
            System.out.println(st2.nextElement());
        }

Also String.split() may help You:

     String[] result = "this is a test".split("\\s");
     for (int x=0; x<result.length; x++)
         System.out.println(result[x]);

OUTPUT:

this
 is
 a
 test



回答11:


System.out.println(Arrays.toString(
    "Many words//separated.by-different\tcharacters"
        .split("\\W+")));
//[Many, words, separated, by, different, characters]


来源:https://stackoverflow.com/questions/13471134/traversing-through-a-sentence-word-by-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!