Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 681
AngryHacker Avatar asked Jan 10 '10 22:01

AngryHacker


People also ask

How do you split a string in R?

To split a string in R, use the strsplit() method. The strsplit() is a built-in R function that splits the string vector into sub-strings. The strsplit() method returns the list, where each list item resembles the item of input that has been split.

What function can be used to split the string in R?

Strsplit(): An R Language function which is used to split the strings into substrings with split arguments.

How do I split a string into string?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.


1 Answers

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')

like image 82
Henk Holterman Avatar answered Nov 14 '22 21:11

Henk Holterman