简体   繁体   English

Apache StringUtils分裂函数可以实现疯狂

[英]Apache StringUtils Split Function does Crazy

I was wondering about this crazy thing in StringUtils.split(string,separator); 我想知道StringUtils.split(string,separator);这个疯狂的事情StringUtils.split(string,separator);

I want to separate a string using a separator @@--@@ and my code goes like this. 我想使用分隔符@@--@@分隔字符串,我的代码就像这样。

String[] strings = StringUtils.split("kayf86@--@9@--@5r43987@!@!%%^&^$%@!@!%-@@*&%$*(&^$%@!@!%--@", "@--@");
for (String string : strings) {
    System.out.println(string);
}

I found the output as such 我发现了输出

kayf86
9
5r43987
!
!%%^&^$%
!
!%
*&%$*(&^$%
!
!%

I use commons-lang-2.6.jar Can some one explain that how this thing had happen. 我使用commons-lang-2.6.jar可以解释一下这件事是怎么发生的。

StringUtils uses any of the characters in the separatorChars argument as the separator, not necessarily the whole thing. StringUtils使用任何的字符separatorChars参数作为分隔符,不一定是整个事情。 The javadoc also states javadoc还说

The separator is not included in the returned String array. 分隔符不包含在返回的String数组中。 Adjacent separators are treated as one separator. 相邻的分隔符被视为一个分隔符。

Parameters 参数

  • separatorChars the characters used as the delimiters, null splits on whitespace spacerChars用作分隔的字符,在空白处为null拆分

Alternatively, you can use StringUtils.splitByWholeSeparator(String, String) to split on the exact @--@ or whatever it is. 或者,您可以使用StringUtils.splitByWholeSeparator(String, String)来拆分精确的@--@或其他任何内容。

In this scenario it takes both @,- as separator so thats why this output is produced. 在这种情况下,它同时使用@,-作为分隔符,因此才产生此输出。 For exact output you have to try some other "separator" 要获得准确的输出,您必须尝试其他“分隔符”

To Refer : http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#split%28java.lang.String,%20java.lang.String%29 要引用: http : //commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#split%28java.lang.String,%20java.lang。字符串29%

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

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