Joe Duffy's Blog implies using string.Substring is more efficient than string.Split.
I don't know if its saying the Substring method does not allocate a new string or if it is just more efficient because it does not make any unneeded allocations. Can you please explain how it is more efficient and show an example.
I understand his first example as creating an array and then processing each of the strings in the array.
string str = ...;
string[] substrs = str.Split(',');
foreach (string subtr in substrs) {
Process(substr);
}
How is the following more efficient
string str = ...;
int lastIndex = 0;
int commaIndex;
while ((commaIndex = str.IndexOf(',', commaIndex)) != -1) {
Process(substr, lastIndex, commaIndex);
lastIndex = commaIndex + 1;
What I see is using String.IndexOf to find the index of the comma then processing the string. I assume he intends to use String.Substring to extract the data during his processing. One of the comments below suggested he may be pulling it character by character. Would he be pulling characters until he hits the next comma possibly building up an array of char?
Substringis not efficient. And I think that "String does, after all, have an indexer. And it’s type-safe! So in-place parsing at least won’t lead to buffer overruns" implies he intends you to access the substring character by character, rather than usingSubstring.Trim*,StartsWith,EndsWith, and straightforward comparisons in general. (For example,substr == "stuff"becomes(end - start) == 5 && String.Compare(str, start, "stuff", 0, 5) == 0.) You'd have to do all that yourself, and you'd probably mess up quite a bit along the way. Note that the author's own example doesn't compile. :PSubstringisn't as efficient as it could be. The code i saw seems to indicate that it always makes a new array containing a copy of the characters, rather than referencing the original string's array using a range like Java does. The drawback to Java's way is thatlongString.substring(0, 2)can keeplongString's entire backing array in memory, even though you only ever use two chars of it oncelongStringdies.