开发者

Convert Text with newlines to a List<String>

开发者 https://www.devze.com 2023-02-03 06:44 出处:网络
I need a way to take a list 开发者_运维知识库of numbers in string form to a List object. Here is an example:

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?


using System.Linq; 
List<string> idList = ids.Split(new[] { "\r\n" }, StringSplitOptions.None)
                             .ToList();


I'd try using ids.split(new[] { "\r\n" }, StringSplitOptions.None)

thus

foreach(string id in ids.Split(new[] { "\r\n" }, StringSplitOptions.None))
{
}


        string ids = "10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19";      
        List<String> idList = Regex.Split(ids, "\r\n").ToList();

        foreach (String id in idList)
        {
            //Do you stuff here  
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消