除法运算符“/”的 StreamTokenizer 行为

StreamTokenizer Behavior for division operator "/"

查看 StreamTokenizer here 的演示代码。当字符串中有 / 时似乎无法正常工作(只需在 StringReader 中的字符串之间添加 / 即可)。这是来自 link,

的代码
StreamTokenizer tokenizer = new StreamTokenizer(
    new StringReader("Mary had 1 little lamb..."));

    while(tokenizer.nextToken() != StreamTokenizer.TT_EOF){

        if(tokenizer.ttype == StreamTokenizer.TT_WORD) {
            System.out.println(tokenizer.sval);
        } else if(tokenizer.ttype == StreamTokenizer.TT_NUMBER) {
            System.out.println(tokenizer.nval);
        } else if(tokenizer.ttype == StreamTokenizer.TT_EOL) {
            System.out.println();
        }
    }

例如,对于字符串 "Mary had 1 little lamb...",输出为

Mary
had
1.0
little
lamb...

对于字符串 "Mary had 1 /little lamb...",输出为

Mary
had
1.0

根据the documentation/StreamTokenizer中的注释字符。在它之后的所有内容,直到 EOL 或 EOF 将被忽略并且不会被标记化。

例如,继续你给出的例子,如果字符串是 "Mary had 1 / 2\n little lamb..."2 被注释掉并且不会被标记化,标记化将在 [= 之后恢复15=]。所以输出将是:

Mary
had
1.0
little
lamb...

只是添加到上面的答案 .由于 / 是 StreamTokenizer 中的注释字符,因此一直持续到行尾。因此,您必须在字符串中添加新行 \n\r 所以 "Mary had 1 / \n little /\nlamb..." 或者 "Mary had 1 / \r little/\rlamb..."两者都适合你