【问题标题】:Whitespace delimiter not working - StringTokenizer空白分隔符不起作用 - StringTokenizer
【发布时间】:2015-03-21 18:23:08
【问题描述】:

我必须使用空格来标记字符串,使用这个构造函数:

StringTokenizer st = new StringTokenizer(str, " ");

我觉得是一样的

StringTokenizer st = new StringTokenizer(str);

问题是,当我尝试使用空格作为分隔符时,它只需要第一个标记,所以输出是这样的:

3 5 + //this is the string which has to be tokenized
3 | //these are the tokens it actually takes 

如果我用逗号(或除空格以外的任何其他分隔符)做同样的事情,那么:

StringTokenizer st = new StringTokenizer(stringa, ",");

它工作正常,它需要我需要的所有令牌:

3,5,+ //this is the string which has to be tokenized
3 | 5 | //these are the tokens it actually takes
8 | //this is the result of the sum

【问题讨论】:

    标签: whitespace token delimiter stringtokenizer


    【解决方案1】:

    找到了解决方案,希望这对某人有所帮助。我正在使用 Scanner 类对要标记的字符串进行标记,但是以错误的方式,我不得不使用 sc.nextLine() 而不是 sc.next()

    【讨论】:

    • 请记住,您可以accept your own answer 向有类似问题的其他用户发出信号,告知有解决该问题的答案。
    猜你喜欢
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多