다음을 통해 공유


방법: 여러 문자열 연결(C# 프로그래밍 가이드)

연결은 한 문자열을 다른 문자열의 끝에 추가하는 프로세스입니다.+ 연산자를 사용하여 문자열 리터럴이나 문자열 상수를 연결하면 컴파일러가 단일 문자열을 생성합니다.런타임 연결은 발생하지 않습니다.하지만 문자열 변수는 런타임에만 연결할 수 있습니다.이 경우 다양한 접근 방식이 성능에 미치는 영향을 이해해야 합니다.

예제

다음 예제에서는 긴 문자열 리터럴을 더 짧은 문자열로 분할하여 소스 코드의 가독성을 향상시키는 방법을 보여 줍니다.분할된 문자열은 컴파일 타임에 단일 문자열로 연결됩니다.이 경우 처리되는 문자열 수가 런타임 성능에 미치는 영향은 없습니다.

static void Main()
{
    // Concatenation of literals is performed at compile time, not run time.
    string text = "Historically, the world of data and the world of objects " +
    "have not been well integrated. Programmers work in C# or Visual Basic " +
    "and also in SQL or XQuery. On the one side are concepts such as classes, " +
    "objects, fields, inheritance, and .NET Framework APIs. On the other side " +
    "are tables, columns, rows, nodes, and separate languages for dealing with " +
    "them. Data types often require translation between the two worlds; there are " +
    "different standard functions. Because the object world has no notion of query, a " +
    "query can only be represented as a string without compile-time type checking or " +
    "IntelliSense support in the IDE. Transferring data from SQL tables or XML trees to " +
    "objects in memory is often tedious and error-prone.";

    Console.WriteLine(text);
}

문자열 변수를 연결하려면 + 또는 += 연산자를 사용하거나 String.Concat, String.Format 또는 StringBuilder.Append 메서드를 사용할 수 있습니다.+ 연산자는 쉽게 사용할 수 있으며 코드 가독성을 높여 줍니다.한 문장에서 + 연산자를 여러 개 사용해도 문자열 내용은 한 번만 복사됩니다.그러나 반복문 등에서 이 연산을 여러 번 반복하면 효율성이 저하될 수도 있습니다.예를 들어, 다음 코드를 참조하십시오.

static void Main(string[] args)
{
    // To run this program, provide a command line string.
    // In Visual Studio, see Project > Properties > Debug.
    string userName = args[0];
    string date = DateTime.Today.ToShortDateString();

    // Use the + and += operators for one-time concatenations.
    string str = "Hello " + userName + ". Today is " + date + ".";
    System.Console.WriteLine(str);

    str += " How are you today?";
    System.Console.WriteLine(str);

    // Keep the console window open in debug mode.
    Console.WriteLine("Press any key to exit.");
    Console.ReadKey();
}

// Example output: 
//  Hello Alexander. Today is 1/22/2008.
//  Hello Alexander. Today is 1/22/2008. How are you today?
//  Press any key to exit.
//

[!참고]

문자열 연결 연산에서 C# 컴파일러는 null 문자열을 빈 문자열과 같은 것으로 처리하지만 원래 null 문자열의 값을 변환하지는 않습니다.

루프에서와 같이 많은 수의 문자열을 연결하는 경우가 아니라면 이 코드의 성능 비용은 그리 크지 않을 것입니다.같은 기준이 String.ConcatString.Format 메서드에도 적용됩니다.

하지만 성능이 중요한 경우에는 항상 StringBuilder 클래스를 사용하여 문자열을 연결해야 합니다.다음 코드에서는 + 연산자와 같은 성능 영향이 없는 StringBuilder 클래스의 Append 메서드를 사용하여 문자열을 연결합니다.

class StringBuilderTest
{
    static void Main()
    {
        string text = null;

        // Use StringBuilder for concatenation in tight loops.
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        for (int i = 0; i < 100; i++)
        {
            sb.AppendLine(i.ToString());
        }
        System.Console.WriteLine(sb.ToString());

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
// Output:
// 0
// 1
// 2
// 3
// 4
// ...
//

참고 항목

참조

String

StringBuilder

개념

C# 프로그래밍 가이드

기타 리소스

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