views:

248

answers:

3

If I have a variable that contains text information (say taken from a textarea), how can I read the text content held in a string variable line by line?

The text entered in the text area will have \n (enter key) to separate the line.

+4  A: 

Try to use

string[] strings = yourString.Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
Hun1Ahpu
This is inefficient for large strings.
SLaks
@SLaks - Nothing was said about the size. So I assume that the size is reasonable.
Hun1Ahpu
You would have serious download and/or injection problems long before this becomes any less efficient than a StringReader.
Henk Holterman
+5  A: 

You can use a StringReader:

var reader = new StringReader(textbox.Text);

string line;
while(null != (line = reader.ReadLine()) {
    //...
}
SLaks
A: 
string[] splitInput = System.Text.RegularExpressions.Regex.Split(
                        InputString, "\r\n");
Glennular
There is _really_ no point in using regular expressions here.
SLaks