简体   繁体   English

StringTokenizer是否在令牌上有最大长度?

[英]Does StringTokenizer have any max length on tokens?

I have a piece of code that has been working for years until today. 我有一段代码一直工作到今天。 After debugging I realized that last token it not collected correctly. 调试后,我意识到最后一个令牌没有正确收集。 I think is because of his length (more than 10k chars). 我认为是因为他的身高(超过1万个字符)。

Code: 码:

StringTokenizer tokens = new StringTokenizer(myString,"&&&&&&&"); 
(...)
String s=tokens.nextToken();
//Do something with s
s=tokens.nextToken();
//Do something with s
s=tokens.nextToken();
//Do something with s

//Now it's time of last and biggest token
s=tokens.nextToken(); // --> s does not contain entire string

You are using the StringTokenizer in the wrong way. 您以错误的方式使用StringTokenizer Your tokenizer does not split at "&&&&&&&" as one would expect, but at '&' , since it just requires one character from your delimiters String to delimit tokens. 您的标记生成器不会像期望的那样在"&&&&&&&"处分割,而是在'&'处分割,因为它只需要从分隔符String中分隔一个字符来分隔标记。 It then discards empty tokens, which is why you got the expected result anyway. 然后,它会丢弃空令牌,这就是为什么无论如何都能获得预期结果的原因。 For example: 例如:

    StringTokenizer tokens = new StringTokenizer("a&&b&&c", "&&&");
    while (tokens.hasMoreTokens()) {
        System.out.println(tokens.nextToken());
    }

This prints: 打印:

a
b
c

So my suspicion is there is an & somewhere within you 10k token. 因此,我怀疑您的1万枚代币中有一个& If that could be the case, I suggest that msaint's suggestion, using String.split() , is the way to go if you can afford modifying your old code. 如果可能的话,我建议使用msaint的建议,即使用String.split() ,如果您有能力修改旧代码的话。

API seems to have no limitation in terms of length. API的长度似乎没有限制。 I tried to reproduce your case and couldn't succeed. 我试图重现您的案件,但未能成功。 I was able to get 7 Mega chars from stringtokenizer. 我从stringtokenizer中获得了7个超级字符。 You can check your string first, then try split as stringtokenizer is a legacy class. 您可以先检查您的字符串,然后尝试分割,因为stringtokenizer是旧类。

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

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