简体   繁体   English

将带有换行符的文本转换为List <String>

[英]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. 我需要一种方法来将字符串形式的数字列表带到List对象。

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? .net库中有什么东西让我自己不必编写SomeCoolMethodToParseTheText吗?

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) 我尝试使用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  
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM