user3717431
user3717431

Reputation: 103

What is the difference between split method in String class and the split method in Apache StringUtils?

I am reading a file line by line and want to split each line on the basis of specific delimiter.I found some options available in String class and StringUtils class.

So my question is which is the better option to use and why?

Upvotes: 6

Views: 5470

Answers (3)

RahulArackal
RahulArackal

Reputation: 954

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

Upvotes: 9

Margit Agerbo Bork
Margit Agerbo Bork

Reputation: 17

It is worth noting that StringUtils.split documentation states: . Adjacent separators are treated as one separator e.g. StringUtils.split("parm1,parm2,,parm4", ",") gives ["parm1", "parm2", "parm4"] If you want ["parm1", "parm2","" ,"parm4"] you need StringUtils.splitPreserveAllTokens

Upvotes: 1

lxcky
lxcky

Reputation: 1668

Well, it really depends on what you want to achieve. Reading the docs for the split method on String and StringUtils, they're quite different from each other. And based on your requirements

...want to split each line on the basis of specific delimiter.

It seems what you need is the split method in String

  • public String[] split(String regex) - Splits this string around matches of the given regular expression. (src)

ex:

String str = "abc def";
str.split(" ");

returns:

["abc", "def"]

Because the one in the StringUtils is:

  • public static String[] split(String str) - Splits the provided text into an array, using whitespace as the separator. (src)

ex:

StringUtils.split("abc def")

returns:

["abc", "def"]

It's an overloaded method though, so you can use the one that takes another argument for the delimiter

  • public static String[] split(String str, char separatorChar) - Splits the provided text into an array, separator specified. This is an alternative to using StringTokenizer.

Upvotes: 1

Related Questions