프로그램 자료/Visual C#

C# 여러줄인 스트링을 라인별로 읽기 Multiline string LineReader

motolies 2015. 11. 11. 10:24

출처 : http://stackoverflow.com/questions/1500194/c-looping-through-lines-of-multiline-string#1500257




아래와 같이 함수를 만들고..


public static List<string> LineReader(this string input)

{

    List<string> rtn = new List<string>();

    using (StringReader reader = new StringReader(input))

    {

        string line;

        while ((line = reader.ReadLine()) != null)

        {

            rtn.Add(line);

        }

    }

    return rtn;

}

 

 

 

아래와 같이 사용한다.

 

foreach (string line in stringData.LineReader())

{

    // Do something with the line

}