새 문자열 만들기

업데이트: 2007년 11월

.NET Framework에서는 단순한 할당을 사용하여 문자열을 만들 수 있고 클래스 생성자를 오버로드하여 서로 다른 여러 매개 변수를 사용한 문자열 생성을 지원할 수 있습니다. 또한 .NET Framework에서는 여러 문자열, 문자열 배열 또는 개체를 결합하여 새 문자열 개체를 만드는 여러 가지 메서드를 System.String 클래스에 제공합니다.

할당을 사용하여 문자열 만들기

String 개체를 만드는 가장 쉬운 방법은 문자열 리터럴을 단순히 String 개체에 할당하는 것입니다.

클래스 생성자를 사용하여 문자열 만들기

String 클래스 생성자의 오버로드를 사용하여 문자 배열에서 문자열을 만들 수 있습니다. 특정 문자를 지정된 횟수만큼 복제하여 새 문자열을 만들 수도 있습니다.

문자열을 반환하는 메서드

다음 표에서는 새 문자열 개체를 반환하는 몇 가지 유용한 메서드를 보여 줍니다.

메서드 이름

용도

String.Format

입력 개체의 집합을 사용하여 형식 지정된 문자열을 만듭니다.

String.Concat

두 개 이상의 기존 문자열을 연결하여 새 문자열을 만듭니다.

String.Join

문자열 배열을 결합하여 새 문자열을 만듭니다.

String.Insert

기존 문자열의 지정된 인덱스에 문자열을 삽입하여 새 문자열을 만듭니다.

String.CopyTo

문자열에서 지정한 문자를 문자 배열의 지정된 위치로 복사합니다.

Format

String.Format 메서드를 사용하여 형식 지정된 문자열을 만들고 여러 개체를 나타내는 문자열을 연결할 수 있습니다. 이 메서드에서는 전달된 개체를 문자열로 자동 변환합니다. 예를 들어, 응용 프로그램에서 사용자에게 Int32 값과 DateTime 값을 표시해야 하는 경우 Format 메서드를 사용하면 간편하게 이러한 값을 표시하는 문자열을 만들 수 있습니다. 이 메서드에 적용하는 형식 지정 규칙에 대한 자세한 내용은 합성 형식 지정에 있는 관련 단원을 참조하십시오.

다음 예제에서는 Format 메서드를 통해 정수 변수를 사용하는 문자열을 만듭니다.

Dim numberOfFleas As Integer = 12
Dim miscInfo As String = String.Format("Your dog has {0} fleas. " & _
                                       "It is time to get a flea collar. " & _ 
                                       "The current universal date is: {1:u}.", _ 
                                       numberOfFleas, Date.Now)
Console.WriteLine(miscInfo)
' The example displays the following output:
'       Your dog has 12 fleas. It is time to get a flea collar. 
'       The current universal date is: 2008-03-28 13:31:40Z.
int numberOfFleas = 12;
string miscInfo = String.Format("Your dog has {0} fleas. " +
                                "It is time to get a flea collar. " + 
                                "The current universal date is: {1:u}.", 
                                numberOfFleas, DateTime.Now);
Console.WriteLine(miscInfo);
// The example displays the following output:
//       Your dog has 12 fleas. It is time to get a flea collar. 
//       The current universal date is: 2008-03-28 13:31:40Z.

이 예제에서 DateTime.Now는 현재 스레드에 연결된 문화권에서 지정하는 방식으로 현재 날짜와 시간을 표시합니다.

Concat

String.Concat 메서드를 사용하면 간편하게 두 개 이상의 기존 개체를 이용하여 새로운 문자열 개체를 만들 수 있습니다. 이 메서드에서는 특정 언어에 국한되지 않는 방식으로 문자열을 연결할 수 있습니다. 이 메서드는 System.Object에서 파생되는 모든 클래스를 받아들입니다. 다음 예제에서는 두 개의 기존 문자열 개체와 구분 문자에서 문자열을 만듭니다.

Dim helloString1 As String = "Hello"
Dim helloString2 As String = "World!"
Console.WriteLine(String.Concat(helloString1, " "c, helloString2))
' The example displays the following output:
'      Hello World!
string helloString1 = "Hello";
string helloString2 = "World!";
Console.WriteLine(String.Concat(helloString1, ' ', helloString2));
// The example displays the following output:
//      Hello World!

Join

String.Join 메서드에서는 문자열 배열과 구분 기호 문자열을 받은 다음 새로운 문자열을 만듭니다. 이 메서드는 여러 개의 문자열을 연결하여 쉼표 등의 구분 기호로 분리된 문자열의 목록을 만들려는 경우에 유용합니다.

다음 예제에서는 공백을 사용하여 문자열 배열을 연결합니다.

Dim words() As String = {"Hello", "and", "welcome", "to", "my" , "world!"}
Console.WriteLine(String.Join(" ", words))
' The example displays the following output:
'      Hello and welcome to my world!
string[] words = {"Hello", "and", "welcome", "to", "my" , "world!"};
Console.WriteLine(String.Join(" ", words));
// The example displays the following output:
//      Hello and welcome to my world!

Insert

String.Insert 메서드를 사용하면 문자열의 지정된 위치에 다른 문자열을 삽입하여 새로운 문자열을 만들 수 있습니다. 이 메서드에서는 0에서 시작하는 인덱스를 사용합니다. 다음 예제에서는 MyString의 다섯 번째 인덱스 위치에 문자열을 삽입하여 새로운 문자열을 만듭니다.

Dim sentence As String = "Once a time."   
 Console.WriteLine(sentence.Insert(4, " upon"))
 ' The example displays the following output:
 '      Once upon a time.
string sentence = "Once a time.";   
 Console.WriteLine(sentence.Insert(4, " upon"));
 // The example displays the following output:
 //      Once upon a time.

CopyTo

String.CopyTo 메서드를 사용하여 문자열의 일부분을 문자 배열에 복사할 수 있습니다. 문자열의 시작 인덱스 및 복사할 문자의 개수를 지정할 수 있습니다. 이 메서드에서는 소스 인덱스, 문자의 배열, 대상 인덱스 및 복사할 문자의 개수를 인수로 사용합니다. 모든 인덱스는 0에서 시작합니다.

다음 예제에서는 CopyTo 메서드를 사용하여 문자열 개체에 포함된 "Hello"라는 단어의 문자를 문자 배열의 첫 번째 인덱스 위치에 복사합니다.

Dim greeting As String = "Hello World!"
Dim charArray() As Char = {"W"c, "h"c, "e"c, "r"c, "e"c}
Console.WriteLine("The original character array: {0}", New String(charArray))
greeting.CopyTo(0, charArray,0 ,5)
Console.WriteLine("The new character array: {0}", New String(charArray))
' The example displays the following output:
'       The original character array: Where
'       The new character array: Hello
string greeting = "Hello World!";
char[] charArray = {'W','h','e','r','e'};
Console.WriteLine("The original character array: {0}", new string(charArray));
greeting.CopyTo(0, charArray,0 ,5);
Console.WriteLine("The new character array: {0}", new string(charArray));
// The example displays the following output:
//       The original character array: Where
//       The new character array: Hello

참고 항목

개념

합성 형식 지정

기타 리소스

기본적인 문자열 작업