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