Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top