다음을 통해 공유


NET에서 새 문자열 만들기

.NET을 사용하면 간단한 할당을 사용하여 문자열을 만들 수 있으며 다양한 매개 변수를 사용하여 문자열 생성을 지원하기 위해 클래스 생성자를 오버로드합니다. .NET은 또한 여러 문자열, 문자열 배열 또는 개체를 결합하여 새 문자열 개체를 만드는 클래스의 여러 메서드 System.String 를 제공합니다.

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

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

클래스 생성자를 사용하여 문자열 만들기Create strings using a class constructor

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

문자열을 반환하는 메서드

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

메서드 이름 사용하세요
String.Format 입력 개체 집합에서 형식이 지정된 문자열을 작성합니다.
String.Concat 둘 이상의 문자열에서 문자열을 빌드합니다.
String.Join 문자열 배열을 결합하여 새 문자열을 빌드합니다.
String.Insert 기존 문자열의 지정된 인덱스에 문자열을 삽입하여 새 문자열을 작성합니다.
String.CopyTo 문자열의 지정된 문자를 문자 배열의 지정된 위치에 복사합니다.

포맷

String.Format 메서드를 사용하여 형식이 지정된 문자열을 만들고 여러 개체를 나타내는 문자열을 연결할 수 있습니다. 이 메서드는 전달된 개체를 문자열로 자동으로 변환합니다. 예를 들어, 응용 프로그램에서 사용자에게 값과 Int32 값을 표시 DateTime 해야 하는 경우 메서드를 사용하여 이러한 값을 나타내는 문자열을 쉽게 생성할 수 있습니다Format. 이 메서드와 함께 사용되는 서식 지정 규칙에 대한 자세한 내용은 복합 서식 지정 섹션을 참조하세요.

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

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

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

Concat

String.Concat 방법을 사용하여 두 개 이상의 기존 개체에서 새 문자열 개체를 쉽게 만들 수 있습니다. 문자열을 연결할 수 있는 언어 독립적 방법을 제공합니다. 이 메서드는 에서 System.Object파생되는 모든 클래스를 허용합니다. 다음 예제에서는 두 개의 기존 문자열 개체와 구분 문자에서 문자열을 만듭니다.

string helloString1 = "Hello";
string helloString2 = "World!";
Console.WriteLine(String.Concat(helloString1, ' ', helloString2));
// The example displays the following output:
//      Hello World!
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.Join 메서드는 문자열 배열과 구분 기호 문자열에서 새 문자열을 만듭니다. 이 메서드는 여러 문자열을 함께 연결하여 목록을 쉼표로 구분하려는 경우에 유용합니다.

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

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!
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.Insert 메서드는 다른 문자열의 지정된 위치에 문자열을 삽입하여 새 문자열을 만듭니다. 이 메서드는 0부터 시작하는 인덱스입니다. 다음은 다섯 번째 인덱스 위치에 MyString 문자열을 삽입하고 이 값을 사용하여 새 문자열을 만드는 예제입니다.

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

복사하기

String.CopyTo 메서드는 문자열의 일부를 문자 배열로 복사합니다. 문자열의 시작 인덱스와 복사할 문자 수를 모두 지정할 수 있습니다. 이 메서드는 원본 인덱스, 문자 배열, 대상 인덱스 및 복사할 문자 수를 사용합니다. 모든 인덱스는 0부터 시작하는 것입니다.

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

string greeting = "Hello World!";
char[] charArray = {'W','h','e','r','e'};
Console.WriteLine($"The original character array: {new string(charArray)}");
greeting.CopyTo(0, charArray,0 ,5);
Console.WriteLine($"The new character array: {new string(charArray)}");
// The example displays the following output:
//       The original character array: Where
//       The new character array: 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

참고하십시오