.Net: Do not Split your Strings

It could result in an “Out of Memory Exception” on really large strings as a colleague experienced last week.

Click to Read More

I admit I used to call Split a lot too as it’s really easy and spares me a bunch of code. Ex.: to parse lines of a CSV file. And yet, the documentation on MSDN is clear about Split performances:

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, and optionally the Compare method, to locate a substring within a string.

Here is a guy who deeply analyzed the memory issue which occurs when splitting a very large string : “Out of Memory Exception

And if you need to parse the content of a large .csv, here is an efficient custom parser that can be used instead of calling Split on each line: “Fast CSV Reader

Loading


Categories:

Tags:


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *