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

时间:2014-12-02 13:48:57

标签: java stringtokenizer

我正在尝试使用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。我在哪里混淆输出。但这是输出的东西...... !!! 实际上我相信输出是单词作为单独的空格(分隔符)。

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

1 个答案:

答案 0 :(得分:0)

您使用的是哪个版本的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