문장을 개별 단어로 구문 분석하는 경우 단어의 양쪽 끝에 공백(공백이라고도 함)이 있는 단어로 끝날 수 있습니다. 이 경우 클래스의 트리밍 메서드 System.String
중 하나를 사용하여 문자열의 지정된 위치에서 임의의 수의 공백 또는 다른 문자를 제거할 수 있습니다. 다음 표에서는 사용 가능한 트리밍 메서드에 대해 설명합니다.
메서드 이름 | 사용하세요 |
---|---|
String.Trim | 문자열의 시작과 끝에서 문자 배열에 지정된 공백 또는 문자를 제거합니다. |
String.TrimEnd | 문자열의 끝에서 문자 배열에 지정된 문자를 제거합니다. |
String.TrimStart | 문자열의 시작 부분에서 문자 배열에 지정된 문자를 제거합니다. |
String.Remove | 문자열의 지정된 인덱스 위치에서 지정된 수의 문자를 제거합니다. |
다듬다
다음 예제와 같이 메서드를 사용하여 String.Trim 문자열의 양쪽 끝에서 공백을 쉽게 제거할 수 있습니다.
string MyString = " Big ";
Console.WriteLine($"Hello{MyString}World!");
string TrimString = MyString.Trim();
Console.WriteLine($"Hello{TrimString}World!");
// The example displays the following output:
// Hello Big World!
// HelloBigWorld!
Dim MyString As String = " Big "
Console.WriteLine("Hello{0}World!", MyString)
Dim TrimString As String = MyString.Trim()
Console.WriteLine("Hello{0}World!", TrimString)
' The example displays the following output:
' Hello Big World!
' HelloBigWorld!
문자열의 시작과 끝에서 문자 배열에 지정한 문자를 제거할 수도 있습니다. 다음 예제에서는 공백 문자, 마침표 및 별표가 제거됩니다.
using System;
public class Example
{
public static void Main()
{
String header = "* A Short String. *";
Console.WriteLine(header);
Console.WriteLine(header.Trim( new Char[] { ' ', '*', '.' } ));
}
}
// The example displays the following output:
// * A Short String. *
// A Short String
Module Example
Public Sub Main()
Dim header As String = "* A Short String. *"
Console.WriteLine(header)
Console.WriteLine(header.Trim({" "c, "*"c, "."c}))
End Sub
End Module
' The example displays the following output:
' * A Short String. *
' A Short String
TrimEnd (끝 공백 제거)
메서드는 String.TrimEnd
문자열의 끝에서 문자를 제거하여 새 문자열 개체를 만듭니다. 제거할 문자를 지정하기 위해 문자 배열이 이 메서드에 전달됩니다. 문자 배열의 요소 순서는 트리밍 작업에 영향을 주지 않습니다. 배열에 지정되지 않은 문자를 찾은 경우 트리밍이 중지됩니다.
다음 예제에서는 메서드를 사용하여 문자열의 마지막 문자를 제거합니다 TrimEnd
. 이 예제에서는 배열의 'r'
문자 순서가 중요하지 않음을 설명하기 위해 문자와 'W'
문자의 위치가 반전됩니다. 이 코드는 MyString
의 마지막 단어와 첫 번째 단어의 일부를 제거합니다.
string MyString = "Hello World!";
char[] MyChar = {'r','o','W','l','d','!',' '};
string NewString = MyString.TrimEnd(MyChar);
Console.WriteLine(NewString);
Dim MyString As String = "Hello World!"
Dim MyChar() As Char = {"r", "o", "W", "l", "d", "!", " "}
Dim NewString As String = MyString.TrimEnd(MyChar)
Console.WriteLine(NewString)
이 코드는 He
콘솔에 표시됩니다.
다음 예제에서는 메서드를 사용하여 문자열의 마지막 단어를 제거합니다 TrimEnd
. 이 코드에서 쉼표는 단어를 Hello
따르고 쉼표는 트리밍할 문자 배열에 지정되지 않으므로 트리밍은 쉼표로 끝납니다.
string MyString = "Hello, World!";
char[] MyChar = {'r','o','W','l','d','!',' '};
string NewString = MyString.TrimEnd(MyChar);
Console.WriteLine(NewString);
Dim MyString As String = "Hello, World!"
Dim MyChar() As Char = {"r", "o", "W", "l", "d", "!", " "}
Dim NewString As String = MyString.TrimEnd(MyChar)
Console.WriteLine(NewString)
이 코드는 Hello,
콘솔에 표시됩니다.
트림스타트
메서드 String.TrimStart
는 기존 문자열 개체의 String.TrimEnd
시작 부분에서 문자를 제거 하 여 새 문자열을 만드는 것을 제외 하 고 메서드와 비슷합니다. 제거할 문자를 지정하기 위해 TrimStart
문자 배열이 메서드에 전달됩니다. 메서드와 TrimEnd
마찬가지로 문자 배열의 요소 순서는 트리밍 작업에 영향을 주지 않습니다. 배열에 지정되지 않은 문자를 찾은 경우 트리밍이 중지됩니다.
다음 예제에서는 문자열의 첫 번째 단어를 제거합니다. 이 예제에서는 배열의 'l'
문자 순서가 중요하지 않음을 설명하기 위해 문자와 'H'
문자의 위치가 반전됩니다.
string MyString = "Hello World!";
char[] MyChar = {'e', 'H','l','o',' ' };
string NewString = MyString.TrimStart(MyChar);
Console.WriteLine(NewString);
Dim MyString As String = "Hello World!"
Dim MyChar() As Char = {"e", "H", "l", "o", " "}
Dim NewString As String = MyString.TrimStart(MyChar)
Console.WriteLine(NewString)
이 코드는 World!
콘솔에 표시됩니다.
제거하십시오
이 메서드는 String.Remove 기존 문자열의 지정된 위치에서 시작하는 지정된 수의 문자를 제거합니다. 이 메서드는 0부터 시작하는 인덱스를 가정합니다.
다음 예제에서는 문자열의 0부터 시작하는 인덱스 위치 5부터 시작하여 문자열에서 10자를 제거합니다.
string MyString = "Hello Beautiful World!";
Console.WriteLine(MyString.Remove(5,10));
// The example displays the following output:
// Hello World!
Dim MyString As String = "Hello Beautiful World!"
Console.WriteLine(MyString.Remove(5, 10))
' The example displays the following output:
' Hello World!
교체합니다
메서드를 호출 String.Replace(String, String) 하고 빈 문자열(String.Empty)을 대체로 지정하여 문자열에서 지정된 문자 또는 부분 문자열을 제거할 수도 있습니다. 다음 예제에서는 문자열에서 모든 쉼표가 제거됩니다.
using System;
public class Example
{
public static void Main()
{
String phrase = "a cold, dark night";
Console.WriteLine($"Before: {phrase}");
phrase = phrase.Replace(",", "");
Console.WriteLine($"After: {phrase}");
}
}
// The example displays the following output:
// Before: a cold, dark night
// After: a cold dark night
Module Example
Public Sub Main()
Dim phrase As String = "a cold, dark night"
Console.WriteLine("Before: {0}", phrase)
phrase = phrase.Replace(",", "")
Console.WriteLine("After: {0}", phrase)
End Sub
End Module
' The example displays the following output:
' Before: a cold, dark night
' After: a cold dark night
참고하십시오
.NET