Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Text with newlines to a List<String>

I need a way to take a list of numbers in string form to a List object.

Here is an example:

string ids = "10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19"; 
List<String> idList = new List<String>();

idList.SomeCoolMethodToParseTheText(ids);  <------+
                                                  |
foreach (string id in idList)                     | 
{                                                 |
   // Do stuff with each id.                      |
}                                                 |
                                                  |
// This is the Method that I need ----------------+

Is there something in the .net library so that I don't have to write the SomeCoolMethodToParseTheText myself?

like image 321
Vaccano Avatar asked Jan 10 '11 20:01

Vaccano


1 Answers

using System.Linq; 
List<string> idList = ids.Split(new[] { "\r\n" }, StringSplitOptions.None)
                             .ToList();
like image 102
jason Avatar answered Nov 11 '22 20:11

jason