Suneeta Singh
Suneeta Singh

Reputation: 272

Traversing through a sentence word by word

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.

Upvotes: 4

Views: 7917

Answers (11)

Bohemian
Bohemian

Reputation: 425053

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.

Upvotes: 3

Kalecser
Kalecser

Reputation: 1143

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

Upvotes: -1

Parth Soni
Parth Soni

Reputation: 11648

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

Upvotes: 0

Srinivas B
Srinivas B

Reputation: 1852

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());
    }
}

Upvotes: 0

npinti
npinti

Reputation: 52185

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.

Upvotes: 1

Mihail Shishkov
Mihail Shishkov

Reputation: 15807

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

Upvotes: 1

Jericho
Jericho

Reputation: 10953

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

Upvotes: 1

jlordo
jlordo

Reputation: 37813

Something like this:

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

Upvotes: 10

Kevin Bowersox
Kevin Bowersox

Reputation: 94469

Try splitting the sentence by whitespace character.

String sentence = "This is a sentence.";

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

Upvotes: 1

jabal
jabal

Reputation: 12347

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

Upvotes: 1

aleation
aleation

Reputation: 4844

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.

Upvotes: 1

Related Questions