How would you split by \r\n if String.Split(String[]) did not exist?

AngryHacker picture AngryHacker · Jan 10, 2010 · Viewed 24k times · Source

Using the .NET MicroFramework which is a really cut-down version of C#. For instance, System.String barely has any of the goodies that we've enjoyed over the years.

I need to split a text document into lines, which means splitting by \r\n. However, String.Split only provides a split by char, not by string.

How can I split a document into lines in an efficient manner (e.g. not looping madly across each char in the doc)?

P.S. System.String is also missing a Replace method, so that won't work.
P.P.S. Regex is not part of the MicroFramework either.

Answer

Henk Holterman picture Henk Holterman · Jan 10, 2010

You can do

string[] lines = doc.Split('\n');
for (int i = 0; i < lines.Length; i+= 1)
   lines[i] = lines[i].Trim();

Assuming that the µF supports Trim() at all. Trim() will remove all whitespace, that might be useful. Otherwise use TrimEnd('\r')