-6

I would like to parse string text line, let's say the words are divided by ','. I read that split method run on the string line twice in order so split some string, so I decided to split by myself and loop on the string line, then concatenation char by char. Example of one iteration:

string[] terms = new string[4];
string term = "";
string line = "home,work,studies,chill";
while(line[index] != ',')
    term = term + line[index];
    index++;
terms[index] = term;

On the other hand, concatenation char by char will create an array of chars every time. So which way is the most efficient?

  • 3
    You should profile it and see. – Evan Trimboli Dec 12 '16 at 21:22
  • 4
    Test it and see for yourself. It's almost certainly more efficient to use `Split`, but I don't know what *"I decided to split by myself and loop on the string line, then concatenation char by char."* is supposed to mean. Maybe some code would help – Matt Burland Dec 12 '16 at 21:22
  • 1
    Bulk concatenation will almost definitely be slower. Strings in .NET are immutable, so any concatenation has to allocate space for a new string and copy the contents from both source strings to that new location. If you were to do bulk concatenation, you should use the `System.Text.StringBuilder` class, which is much more memory efficient for that specific task. – JamesFaix Dec 12 '16 at 21:24
  • Very tempted to close as duplicate of http://stackoverflow.com/questions/11133185/stringbuilder-versus-string-concat... In any case please read https://ericlippert.com/2012/12/17/performance-rant/ before asking any performance questions. – Alexei Levenkov Dec 12 '16 at 22:24

1 Answers1

0

If you need all the elements that are between the delimiters, then Split() is the way to go.

If you only need, say, the second element, and you don't want to allocate memory for the first and third elements, you can use IndexOf to find the delimiters then extract the string using SubString.

More tips on performance can be found here:

Performance Considerations

The Split methods allocate memory for the returned array object and a String object for each array element. If your application requires optimal performance or if managing memory allocation is critical in your application, consider using the IndexOf or IndexOfAny method. You also have the option of using the Compare method to locate a substring within a string.

To split a string at a separator character, use the IndexOf or IndexOfAny method to locate a separator character in the string. To split a string at a separator string, use the IndexOf or IndexOfAny method to locate the first character of the separator string. Then use the Compare method to determine whether the characters after that first character are equal to the remaining characters of the separator string.

In addition, if the same set of characters is used to split strings in multiple Split method calls, consider creating a single array and referencing it in each method call. This significantly reduces the additional overhead of each method call.

Community
  • 1
  • 1
John Wu
  • 50,556
  • 8
  • 44
  • 80