What is the difference between split method in String class and the split method in Apache StringUtils?
It depends on the use case.
What's the difference ?
String[] split(String regEx)
String[] results = StringUtils.split(String str,String separatorChars)
Apache utils split() is null safe.
StringUtils.split(null)
will returnnull
. The JDK default is not null safe:try{ String testString = null; String[] result = testString.split("-"); System.out.println(result.length); } catch(Exception e) { System.out.println(e); // results NPE }
The default String#split() uses a regular expression for splitting the string.
The Apache version StringUtils#split() uses whitespace/char/String characters/null [depends on split() method signature].
Since complex regular expressions are very expensive when using extensively, the defaultString.split()
would be a bad idea. Otherwise it's better.When used for tokenizing a string like following string.split() returns an additional empty string. while Apache version gave the correct results
String testString = "$Hello$Dear$";
String[] result = testString.split("\\$");
System.out.println("Length is "+ result.length); //3
int i=1;
for(String str : result) {
System.out.println("Str"+(i++)+" "+str);
}
Output
Length is 3 Str1 Str2 Hello Str3 Dear
String[] result = StringUtils.split(testString,"$");
System.out.println("Length is "+ result.length); // 2
int i=1;
for(String str : result) {
System.out.println("Str"+(i++)+" "+str);
}
Output
Length is 2 Str1 Hello Str2 Dear