Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Easiest way to split a string on newlines in .NET?

I need to part a string into newlines in .NET and the lone way I am aware of to divide strings is with the Split strategy. Anyway that won't permit me to (effectively) split on a newline, so what is the most ideal approach to do it?
by

2 Answers

sandhya6gczb
To split on a string you need to use the overload that takes an array of strings:

string[] lines = theText.Split(
new[] { Environment.NewLine },
StringSplitOptions.None
);
pankajshivnani123
What about using a StringReader?

using (System.IO.StringReader reader = new System.IO.StringReader(input)) {
string line = reader.ReadLine();
}

Login / Signup to Answer the Question.