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)

  1. Apache utils split() is null safe. StringUtils.split(null) will return null. 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 }

  2. 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 default String.split() would be a bad idea. Otherwise it's better.

  3. 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