Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 412
user3717431 Avatar asked Sep 20 '14 06:09

user3717431


3 Answers

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
like image 152
RahulArackal Avatar answered Nov 10 '22 12:11

RahulArackal


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.
like image 20
lxcky Avatar answered Nov 10 '22 11:11

lxcky


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

like image 35
Margit Agerbo Bork Avatar answered Nov 10 '22 11:11

Margit Agerbo Bork