W tym artykule

Przeciążenia

Usuwa wszystkie wiodące wystąpienia zestawu znaków określone w tablicy z bieżącego String obiektu.Removes all leading occurrences of a set of characters specified in an array from the current String object.

TrimStart()TrimStart()TrimStart()TrimStart()

Zwraca

TrimStart(Char[])TrimStart(Char[])TrimStart(Char[])TrimStart(Char[])

Usuwa wszystkie wiodące wystąpienia zestawu znaków określone w tablicy z bieżącego String obiektu.Removes all leading occurrences of a set of characters specified in an array from the current String object.

Przykłady

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

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

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

Poniższy przykład ilustruje wywołanie StripComments metody.The following example then illustrates a call to the StripComments method.

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 Jeśli metoda usuwa wszystkie znaki z bieżącego wystąpienia, ta metoda nie modyfikuje wartości bieżącego wystąpienia.If the TrimStart method removes any characters from the current instance, this method does not modify the value of the current instance.Zamiast tego zwraca nowy ciąg, w którym zostaną usunięte wszystkie wiodące białe znaki znajdujące się w bieżącym wystąpieniu.Instead, it returns a new string in which all leading white space characters found in the current instance are removed.