简体   繁体   中英

Apache StringUtils Split Function does Crazy

I was wondering about this crazy thing in 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.

StringUtils uses any of the characters in the separatorChars argument as the separator, not necessarily the whole thing. The javadoc also states

The separator is not included in the returned String array. Adjacent separators are treated as one separator.

Parameters

  • separatorChars the characters used as the delimiters, null splits on whitespace

Alternatively, you can use StringUtils.splitByWholeSeparator(String, String) to split on the exact @--@ or whatever it is.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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