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

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

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

+6
source share
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 uses split () - absolutely safe. StringUtils.split(null) will return null . By default, the JDK is not null:

    try{ String testString = null; String[] result = testString.split("-"); System.out.println(result.length); } catch(Exception e) { System.out.println(e);//results NPE }

  2. String # split () by default uses regular expression to split the string.
    Apache's StringUtils # split () version uses whitespace / char / String / null characters [depends on the signature of the split () method].
    Since complex regexes are very expensive in heavy use, by default String.split() would be a bad idea. Otherwise, it’s better.

  3. When used for tokenizing a string like string.split () returns an additional empty string. while the 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); } 

Exit

 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); } 

Exit

 Length is 2 Str1 Hello Str2 Dear 
+9
source

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

... want to split each line based on a specific separator.

It seems you need a split method in String

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

Example:

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

returns:

 ["abc", "def"] 

Because in StringUtils it is:

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

Example:

 StringUtils.split("abc def") 

returns:

 ["abc", "def"] 

This is an overloaded method, so you can use one that takes a different argument for the separator

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

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

+1
source

Source: https://habr.com/ru/post/1203024/


All Articles