繁体   English   中英

我无法在Java中使用StringTokenizer将字符串拆分为令牌

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

我正在尝试使用StringTokenizer将字符串拆分为令牌。

我的代码是:

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

当我运行此代码输出是:

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

我使用空格(+-*/(),.? )作为分隔符。 有一个词叫kids are 我在哪里得到混乱的输出。 但这是输出的东西... !!! 实际上,我相信输出都是两个单词,以空格分隔(定界符)。

为什么我得到这种类型的输出?

您正在使用哪个Java版本。

尝试使用Java 7并显示正确的输出。

http://ideone.com/JSW7Ki

输出形式如下:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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