简体   繁体   中英

I can't split string into token using StringTokenizer in java

I am trying to split string into token using StringTokenizer.

My code is :

    String original = "Short stories for kids are adventurous and interesting ways to teach your children about good morals and right conduct.";
    String delimiters = "+-*/(),.? ";
    StringTokenizer st = new StringTokenizer(original, delimiters);
    while (st.hasMoreTokens()) {
    String w = st.nextToken();
    System.out.println("tokens are = = = "+w);
    }

When i run this code output is :

token are = = = Short
token are = = = stories
token are = = = for
token are = = = kids are
token are = = = adventurous
token are = = = and
token are = = = interesting
token are = = = ways
token are = = = to
token are = = = teach
token are = = = your
token are = = = children
token are = = = about
token are = = = good
token are = = = morals
token are = = = and
token are = = = right
token are = = = conduct

I am used space (+-*/(),.? ) as a delimiter. There is one word called kids are . Where i am getting confused output. But it is something socked output...!!! Actually i believe the output is both word as a separate out by space (delimiter).

Why i am getting this type of output ?

Which version of Java are you using.

Tried with Java 7 and it seems to print the right output.

http://ideone.com/JSW7Ki

Output in this form:

tokens are = = =Short
tokens are = = =stories
tokens are = = =for
tokens are = = =kids
tokens are = = =are
tokens are = = =adventurous
tokens are = = =and
tokens are = = =interesting
tokens are = = =ways
tokens are = = =to
tokens are = = =teach
tokens are = = =your
tokens are = = =children
tokens are = = =about
tokens are = = =good
tokens are = = =morals
tokens are = = =and
tokens are = = =right
tokens are = = =conduct

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM