2011-01-10 6 views
1

문자열 형식의 숫자 목록을 List 개체로 가져 오는 방법이 필요합니다. 나는 SomeCoolMethodToParseTheText 자신을 작성하지 않도록 .NET 라이브러리에 뭔가줄 바꾸기가있는 텍스트를 목록으로 변환 <String>

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 ----------------+ 

있습니까 : 여기

은 예입니다?

+0

이 식별자는 항상 숫자 있습니까 사용해보십시오 것? – VoodooChild

+0

@VoodooChild - 예 – Vaccano

+0

SomeCoolMethodToParseTheText === 나누기 – Hogan

답변

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

'new [] { "\ r \ n"}'문자열의 1 요소 배열을 만들지 않습니까? – Hogan

+4

@Hogan : 네, 그렇습니다. 이 'Split' 오버라이드를 호출합니다 : http://msdn.microsoft.com/en-us/library/tabh47cf.aspx – Gabe

+0

위대한 일을했습니다! 감사. 나는 미래의 작업을 위해 도구 벨트에 분할을 추가 할 것이다. – Vaccano

0

나는 ids.split(new[] { "\r\n" }, StringSplitOptions.None)

따라서

foreach(string id in ids.Split(new[] { "\r\n" }, StringSplitOptions.None)) 
{ 
} 
0
 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 
     } 
관련 문제