It depends on what you want to do. Another option, which is probably redundant for small lists, but might be more memory efficient for large strings, is to use the StringReader class and use an enumerator:
IEnumerable<string> GetNextString(string input) { using (var sr = new StringReader(input)) { string s; while ((s = sr.ReadLine()) != null) { yield return s; } } }
This supports both \n and \r\n line endings. Since it returns IEnumerable you can process it using foreach or use any of the standard linq extensions ( ToList() , ToArray() , Where , etc.).
For example, with foreach :
var ss = "Hello\nworld\r\ntwo bags\r\nsugar"; foreach (var s in GetNextString(ss)) { Console.WriteLine("==> {0}", s); }
Chris J Jan 08 '19 at 20:51 2019-01-08 20:51
source share