다음을 통해 공유


방법: 문자열 분할(C# 프로그래밍 가이드)

다음 코드 예제에서는 String.Split 메서드를 사용하여 문자열을 구문 분석하는 방법을 보여 줍니다.Split에서는 구분 기호로 사용할 문자를 나타내는 문자 배열을 입력으로 받습니다.이 예제에서는 공백, 쉼표, 마침표, 콜론 및 탭이 사용됩니다.이들 구분 기호를 포함하는 배열이 Split에 전달되고, 결과로 반환되는 문자열 배열을 사용하여 문장의 각 단어가 개별적으로 표시됩니다.

예제

class TestStringSplit
{
    static void Main()
    {
        char[] delimiterChars = { ' ', ',', '.', ':', '\t' };

        string text = "one\ttwo three:four,five six seven";
        System.Console.WriteLine("Original text: '{0}'", text);

        string[] words = text.Split(delimiterChars);
        System.Console.WriteLine("{0} words in text:", words.Length);

        foreach (string s in words)
        {
            System.Console.WriteLine(s);
        }

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
/* Output:
    Original text: 'one     two three:four,five six seven'
    7 words in text:
    one
    two
    three
    four
    five
    six
    seven
 */

참고 항목

개념

C# 프로그래밍 가이드

기타 리소스

문자열(C# 프로그래밍 가이드)

.NET Framework 정규식