String Tokenizer issue

半腔热情 提交于 2019-12-13 04:44:54

问题


AM using string tokenizer to delimit the string response by ^

12/30/2011 12:00:00 AM^President^^^159^True^True^True^True^True^False^False^True^True^3/18/2011 12:00:00 AM^True^Jujama, Inc.^^^^True^True

but the problem is when ^ delimiter consecutively its skipping that one and adding in to array. But i want to add space if two ^delimiters comes.

How to do that?

My code is:

 StringTokenizer tokens = new StringTokenizer(partId, "^");

              while(tokens.hasMoreTokens()){

                 String value=tokens.nextToken();
                 userValues.add(value);
                 System.out.println("..."+value);

              }   

回答1:


User string.split("^") instead. Split receives regex, so you can do almost what you want within one line.




回答2:


Check out StringUtils from Apache:

http://commons.apache.org/lang/api-2.3/org/apache/commons/lang/StringUtils.html#splitPreserveAllTokens(java.lang.String, char)



来源:https://stackoverflow.com/questions/6691751/string-tokenizer-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!