String.substring vs String[].split

前端 未结 5 1864
醉梦人生
醉梦人生 2021-01-17 09:24

I have a comma delaminated string that when calling String.split(\",\") it returns an array size of about 60. In a specific use case I only need to get the valu

相关标签:
5条回答
  • 2021-01-17 09:59

    My first inclination would be to find the index of the first and second commas and take the substring.

    The only real way to tell for sure, though, is to test each in your particular scenario. Break out the appropriate stopwatch and measure the two.

    0 讨论(0)
  • 2021-01-17 10:02

    ofcourse why iterate through whole string, just use substring() and indexOf()

    0 讨论(0)
  • 2021-01-17 10:03

    You are certainly better off doing it by hand for two reasons:

    • .split() takes a string as an argument, but this string is interpreted as a Pattern, and for your use case Pattern is costly;
    • as you say, you only need the second element: the algorithm to grab that second element is simple enough to do by hand.
    0 讨论(0)
  • 2021-01-17 10:13

    Since String.Split returns a string[], using a 60-way Split would result in about sixty needless allocations per line. Split goes through your entire string, and creates sixty new object plus the array object itself. Of these sixty one objects you keep exactly one, and let garbage collector deal with the remaining sixty.

    If you are calling this in a tight loop, a substring would definitely be more efficient: it goes through the portion of your string up to the second comma ,, and then creates one new object that you keep.

    String s = "quick,brown,fox,jumps,over,the,lazy,dog";
    int from = s.indexOf(',');
    int to = s.indexOf(',', from+1);
    String brown = s.substring(from+1, to);
    

    The above prints brown

    When you run this multiple times, the substring wins on time hands down: 1,000,000 iterations of split take 3.36s, while 1,000,000 iterations of substring take only 0.05s. And that's with only eight components in the string! The difference for sixty components would be even more drastic.

    0 讨论(0)
  • 2021-01-17 10:18

    I would use something like:

    final int first = searchString.indexOf(",");
    final int second = searchString.indexOf(",", first+1);
    String result= searchString.substring(first+1, second);
    
    0 讨论(0)
提交回复
热议问题