简体   繁体   中英

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?

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 

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM