简体   繁体   English

String类中的split方法和Apache StringUtils中的split方法有什么区别?

[英]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. 我正在逐行读取文件,并希望根据特定的分隔符拆分每一行。我在String类和StringUtils类中找到了一些选项。

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. Apache utils split()是null安全的。 StringUtils.split(null) will return null . StringUtils.split(null)将返回null The JDK default is not null safe: JDK默认值不是null安全的:

    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. 默认的String#split()使用正则表达式来拆分字符串。
    The Apache version StringUtils#split() uses whitespace/char/String characters/null [depends on split() method signature]. Apache版本StringUtils#split()使用whitespace / char / String characters / null [取决于split()方法签名]。
    Since complex regular expressions are very expensive when using extensively, the default String.split() would be a bad idea. 由于复杂的正则表达式在广泛使用时非常昂贵,因此默认的String.split()将是个坏主意。 Otherwise it's better. 否则它会更好。

  3. When used for tokenizing a string like following string.split() returns an additional empty string. 当用于标记字符串时,如下面的string.split()返回一个额外的空字符串。 while Apache version gave the correct results 而Apache版本给出了正确的结果

     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. 读取StringStringUtils上的split方法的文档,它们彼此完全不同。 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 看来你需要的是Stringsplit方法

  • public String[] split(String regex) - Splits this string around matches of the given regular expression. public String[] split(String regex) - 围绕给定正则表达式的匹配拆分此字符串。 (src) (SRC)

ex: 例如:

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

returns: 收益:

["abc", "def"]

Because the one in the StringUtils is: 因为StringUtils中的那个是:

  • public static String[] split(String str) - Splits the provided text into an array, using whitespace as the separator. public static String[] split(String str) - 使用空格作为分隔符,将提供的文本拆分为数组。 (src) (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. public static String[] split(String str, char separatorChar) - 将提供的文本拆分为数组,指定分隔符。 This is an alternative to using StringTokenizer . 这是使用StringTokenizer的替代方法。

It is worth noting that StringUtils.split documentation states: . 值得注意的是StringUtils.split文档说明:。 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 相邻的分隔符被视为一个分隔符,例如StringUtils.split(“parm1,parm2,parm4”,“,”)给出[“parm1”,“parm2”,“parm4”]如果你想要[“parm1”,“parm2” ,“”,“parm4”]你需要StringUtils.splitPreserveAllTokens

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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