String.TrimStart 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
오버로드
TrimStart(ReadOnlySpan<Char>) | |
TrimStart() |
현재 문자열에서 선행 공백 문자를 모두 제거합니다. |
TrimStart(Char) |
현재 문자열에서 지정된 문자의 선행 항목을 모두 제거합니다. |
TrimStart(Char[]) |
현재 문자열에서 배열에 지정된 문자 집합의 선행 항목을 모두 제거합니다. |
TrimStart(ReadOnlySpan<Char>)
public:
System::String ^ TrimStart(ReadOnlySpan<char> trimChars);
public string TrimStart (scoped ReadOnlySpan<char> trimChars);
member this.TrimStart : ReadOnlySpan<char> -> string
Public Function TrimStart (trimChars As ReadOnlySpan(Of Char)) As String
매개 변수
- trimChars
- ReadOnlySpan<Char>
반환
적용 대상
TrimStart()
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
현재 문자열에서 선행 공백 문자를 모두 제거합니다.
public:
System::String ^ TrimStart();
public string TrimStart ();
member this.TrimStart : unit -> string
Public Function TrimStart () As String
반환
공백 문자가 현재 문자열의 시작 부분에서 모두 제거된 후 남아 있는 문자열입니다. 현재 인스턴스에서 어떠한 문자도 삭제할 수 없는 경우 이 메서드는 현재 인스턴스를 반환합니다.
설명
메서드는 TrimStart
현재 문자열에서 모든 선행 공백 문자를 제거합니다. 공백이 아닌 문자가 발견되면 트리밍 작업이 중지됩니다. 예를 들어 현재 문자열이 " abc xyz "이면 메서드는 TrimStart
"abc xyz "를 반환합니다.
참고
메서드가 TrimStart
현재 instance 문자를 제거하는 경우 이 메서드는 현재 instance 값을 수정하지 않습니다. 대신 현재 instance 있는 모든 선행 공백 문자가 제거되는 새 문자열을 반환합니다.
적용 대상
TrimStart(Char)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
현재 문자열에서 지정된 문자의 선행 항목을 모두 제거합니다.
public:
System::String ^ TrimStart(char trimChar);
public string TrimStart (char trimChar);
member this.TrimStart : char -> string
Public Function TrimStart (trimChar As Char) As String
매개 변수
- trimChar
- Char
제거할 유니코드 문자입니다.
반환
trimChar
문자의 항목이 현재 문자열의 시작 부분에서 모두 제거된 후 남아 있는 문자열입니다. 현재 인스턴스에서 어떠한 문자도 삭제할 수 없는 경우 이 메서드는 현재 인스턴스를 반환합니다.
설명
메서드는 TrimStart(System.Char)
현재 문자열에서 모든 선행 trimChar
문자를 제거합니다. 트리밍 작업은 없는 문자가 trimChar
발견되면 중지됩니다. 예를 들어 가 -
이고 현재 문자열이 "---abc---xyz----"인 경우 trimChar
메서드는 TrimStart(System.Char)
"abc---xyz----"를 반환합니다.
참고
메서드가 TrimStart(System.Char)
현재 instance 문자를 제거하는 경우 이 메서드는 현재 instance 값을 수정하지 않습니다. 대신 현재 instance 있는 모든 선행 trimChar
문자가 제거되는 새 문자열을 반환합니다.
적용 대상
TrimStart(Char[])
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
현재 문자열에서 배열에 지정된 문자 집합의 선행 항목을 모두 제거합니다.
public:
System::String ^ TrimStart(... cli::array <char> ^ trimChars);
public string TrimStart (params char[] trimChars);
public string TrimStart (params char[]? trimChars);
member this.TrimStart : char[] -> string
Public Function TrimStart (ParamArray trimChars As Char()) As String
매개 변수
- trimChars
- Char[]
제거할 유니코드 문자 배열이거나 null
입니다.
반환
trimChars
매개 변수의 문자가 현재 문자열의 시작 부분에서 모두 제거된 후 남아 있는 문자열입니다. trimChars
가 null
이거나 빈 배열이면 공백 문자가 대신 제거됩니다. 현재 인스턴스에서 어떠한 문자도 삭제할 수 없는 경우 이 메서드는 현재 인스턴스를 반환합니다.
예제
다음 예제에서는 메서드의 TrimStart 기본 기능을 보여 줍니다.
// TrimStart examples
string lineWithLeadingSpaces = " Hello World!";
string lineWithLeadingSymbols = "$$$$Hello World!";
string lineWithLeadingUnderscores = "_____Hello World!";
string lineWithLeadingLetters = "xxxxHello World!";
string lineAfterTrimStart = string.Empty;
// Make it easy to print out and work with all of the examples
string[] lines = { lineWithLeadingSpaces, lineWithLeadingSymbols, lineWithLeadingUnderscores, lineWithLeadingLetters };
foreach (var line in lines)
{
Console.WriteLine($"This line has leading characters: {line}");
}
// Output:
// This line has leading characters: Hello World!
// This line has leading characters: $$$$Hello World!
// This line has leading characters: _____Hello World!
// This line has leading characters: xxxxHello World!
// A basic demonstration of TrimStart in action
lineAfterTrimStart = lineWithLeadingSpaces.TrimStart(' ');
Console.WriteLine($"This is the result after calling TrimStart: {lineAfterTrimStart}");
// This is the result after calling TrimStart: Hello World!
// Since TrimStart accepts a character array of leading items to be removed as an argument,
// it's possible to do things like trim multiple pieces of data that each have different
// leading characters,
foreach (var lineToEdit in lines)
{
Console.WriteLine(lineToEdit.TrimStart(' ', '$', '_', 'x'));
}
// Result for each: Hello World!
// or handle pieces of data that have multiple kinds of leading characters
var lineToBeTrimmed = "__###__ John Smith";
lineAfterTrimStart = lineToBeTrimmed.TrimStart('_', '#', ' ');
Console.WriteLine(lineAfterTrimStart);
// Result: John Smith
// TrimStart examples
let lineWithLeadingSpaces = " Hello World!"
let lineWithLeadingSymbols = "$$$$Hello World!"
let lineWithLeadingUnderscores = "_____Hello World!"
let lineWithLeadingLetters = "xxxxHello World!"
// Make it easy to print out and work with all of the examples
let lines = [| lineWithLeadingSpaces; lineWithLeadingSymbols; lineWithLeadingUnderscores; lineWithLeadingLetters |]
for line in lines do
printfn $"This line has leading characters: {line}"
// Output:
// This line has leading characters: Hello World!
// This line has leading characters: $$$$Hello World!
// This line has leading characters: _____Hello World!
// This line has leading characters: xxxxHello World!
// A basic demonstration of TrimStart in action
let lineAfterTrimStart = lineWithLeadingSpaces.TrimStart ' '
printfn $"This is the result after calling TrimStart: {lineAfterTrimStart}"
// This is the result after calling TrimStart: Hello World!
// Since TrimStart accepts a character array of leading items to be removed as an argument,
// it's possible to do things like trim multiple pieces of data that each have different
// leading characters,
for lineToEdit in lines do
printfn $"""{lineToEdit.TrimStart(' ', '$', '_', 'x')}"""
// Result for each: Hello World!
// or handle pieces of data that have multiple kinds of leading characters
let lineToBeTrimmed = "__###__ John Smith"
let lineAfterTrimStart2 = lineToBeTrimmed.TrimStart('_', '#', ' ')
printfn $"{lineAfterTrimStart2}"
// Result: John Smith
Public Sub Main()
' TrimStart Examples
Dim lineWithLeadingSpaces as String = " Hello World!"
Dim lineWithLeadingSymbols as String = "$$$$Hello World!"
Dim lineWithLeadingUnderscores as String = "_____Hello World!"
Dim lineWithLeadingLetters as String = "xxxxHello World!"
Dim lineAfterTrimStart = String.Empty
' Make it easy to print out and work with all of the examples
Dim lines As String() = { lineWithLeadingSpaces, line lineWithLeadingSymbols, lineWithLeadingUnderscores, lineWithLeadingLetters }
For Each line As String in lines
Console.WriteLine($"This line has leading characters: {line}")
Next
' Output:
' This line has leading characters: Hello World!
' This line has leading characters: $$$$Hello World!
' This line has leading characters: _____Hello World!
' This line has leading characters: xxxxHello World!
Console.WriteLine($"This line has leading spaces: {lineWithLeadingSpaces}")
' This line has leading spaces: Hello World!
' A basic demonstration of TrimStart in action
lineAfterTrimStart = lineWithLeadingSpaces.TrimStart(" "c)
Console.WriteLine($"This is the result after calling TrimStart: {lineAfterTrimStart}")
' This is the result after calling TrimStart: Hello World!
' Since TrimStart accepts a character array of leading items to be removed as an argument,
' it's possible to do things like trim multiple pieces of data that each have different
' leading characters,
For Each lineToEdit As String in lines
Console.WriteLine(lineToEdit.TrimStart(" "c, "$"c, "_"c, "x"c ))
Next
' Result for each: Hello World!
' or handle pieces of data that have multiple kinds of leading characters
Dim lineToBeTrimmed as String = "__###__ John Smith"
lineAfterTrimStart = lineToBeTrimmed.TrimStart("_"c , "#"c , " "c)
Console.WriteLine(lineAfterTrimStart)
' Result: John Smith
End Sub
다음 예제에서는 메서드를 TrimStart 사용하여 소스 코드 줄에서 공백 및 주석 문자를 트리밍합니다. 메서드는 StripComments
호출 TrimStart 을 래핑하고 공백과 주석 문자(Visual Basic의 경우 아포스트로피( ' ) 및 C# 또는 F#의 슬래시( / )를 포함하는 문자 배열을 전달합니다. TrimStart 문자열이 주석인지 여부를 평가할 때 선행 공백을 제거하기 위해 메서드도 호출됩니다.
public static string[] StripComments(string[] lines)
{
List<string> lineList = new List<string>();
foreach (string line in lines)
{
if (line.TrimStart(' ').StartsWith("//"))
lineList.Add(line.TrimStart(' ', '/'));
}
return lineList.ToArray();
}
let stripComments (lines: #seq<string>) =
[| for line in lines do
if line.TrimStart(' ').StartsWith "//" then
line.TrimStart(' ', '/') |]
Public Shared Function StripComments(lines() As String) As String()
Dim lineList As New List(Of String)
For Each line As String In lines
If line.TrimStart(" "c).StartsWith("'") Then
linelist.Add(line.TrimStart("'"c, " "c))
End If
Next
Return lineList.ToArray()
End Function
다음 예제에서는 StripComments
메서드를 호출하는 방법을 보여 줍니다.
public static void Main()
{
string[] lines = {"using System;",
"",
"public class HelloWorld",
"{",
" public static void Main()",
" {",
" // This code displays a simple greeting",
" // to the console.",
" Console.WriteLine(\"Hello, World.\");",
" }",
"}"};
Console.WriteLine("Before call to StripComments:");
foreach (string line in lines)
Console.WriteLine(" {0}", line);
string[] strippedLines = StripComments(lines);
Console.WriteLine("After call to StripComments:");
foreach (string line in strippedLines)
Console.WriteLine(" {0}", line);
}
// This code produces the following output to the console:
// Before call to StripComments:
// using System;
//
// public class HelloWorld
// {
// public static void Main()
// {
// // This code displays a simple greeting
// // to the console.
// Console.WriteLine("Hello, World.");
// }
// }
// After call to StripComments:
// This code displays a simple greeting
// to the console.
let lines =
[| "module HelloWorld"
""
"[<EntryPoint>]"
"let main _ ="
" // This code displays a simple greeting"
" // to the console."
" printfn \"Hello, World.\""
" 0" |]
printfn "Before call to StripComments:"
for line in lines do
printfn $" {line}"
let strippedLines = stripComments lines
printfn "After call to StripComments:"
for line in strippedLines do
printfn $" {line}"
// This code produces the following output to the console:
// Before call to StripComments:
// module HelloWorld
//
// [<EntryPoint>]
// let main _ =
// // This code displays a simple greeting
// // to the console.
// printfn "Hello, World."
// 0
// After call to StripComments:
// This code displays a simple greeting
// to the console.
Public Shared Sub Main()
Dim lines() As String = {"Public Module HelloWorld", _
" Public Sub Main()", _
" ' This code displays a simple greeting", _
" ' to the console.", _
" Console.WriteLine(""Hello, World."")", _
" End Sub", _
" End Module"}
Console.WriteLine("Code before call to StripComments:")
For Each line As String In lines
Console.WriteLine(" {0}", line)
Next
Dim strippedLines() As String = StripComments(lines)
Console.WriteLine("Code after call to StripComments:")
For Each line As String In strippedLines
Console.WriteLine(" {0}", line)
Next
End Sub
' This code produces the following output to the console:
' Code before call to StripComments:
' Public Module HelloWorld
' Public Sub Main()
' ' This code displays a simple greeting
' ' to the console.
' Console.WriteLine("Hello, World.")
' End Sub
' End Module
' Code after call to StripComments:
' This code displays a simple greeting
' to the console.
설명
메서드는 TrimStart(System.Char[])
현재 문자열에서 매개 변수에 있는 trimChars
모든 선행 문자를 제거합니다. 에 없는 trimChars
문자가 발견되면 트리밍 작업이 중지됩니다. 예를 들어 현재 문자열이 "123abc456xyz789"이고 trimChars
"1"에서 "9 TrimStart(System.Char[])
"의 숫자를 포함하는 경우 메서드는 "abc456xyz789"를 반환합니다.
참고
메서드가 TrimStart(System.Char[])
현재 instance 문자를 제거하는 경우 이 메서드는 현재 instance 값을 수정하지 않습니다. 대신 현재 instance 있는 매개 변수에 trimChars
있는 모든 선행 문자가 제거되는 새 문자열을 반환합니다.
호출자 참고
.NET Framework 3.5 SP1 및 이전 버전은 이 메서드가 가 또는 빈 배열인 경우 trimChars
null
트리밍하는 공백 문자의 내부 목록을 유지 관리합니다. .NET Framework 4부터 가 null
또는 빈 배열인 경우 trimChars
메서드는 모든 유니코드 공백 문자(즉, 메서드에 전달될 IsWhiteSpace(Char) 때 반환 값을 생성하는 true
문자)를 트리밍합니다. 이 변경 Trim() 으로 인해 .NET Framework 3.5 SP1 및 이전 버전의 메서드는 두 문자( ZERO WIDTH SPACE(U+200B) 및 U+FEFF(ZERO WIDTH NO-BREAK SPACE))Trim()를 제거하며, .NET Framework 4 이상 버전의 메서드는 제거되지 않습니다. 또한 Trim() .NET Framework 3.5 SP1 및 이전 버전의 메서드는 세 개의 유니코드 공백 문자인 몽골 모음 구분 기호(U+180E), 좁은 중단 없는 공간(U+202F) 및 중간 수학 공간(U+205F)을 트리밍하지 않습니다.
추가 정보
적용 대상
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기