String.TrimStart Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Przeciążenia
TrimStart(ReadOnlySpan<Char>) | |
TrimStart() |
Usuwa wszystkie wiodące znaki odstępu z bieżącego ciągu. |
TrimStart(Char) |
Usuwa wszystkie wiodące wystąpienia określonego znaku z bieżącego ciągu. |
TrimStart(Char[]) |
Usuwa wszystkie wiodące wystąpienia zestawu znaków określonych w tablicy z bieżącego ciągu. |
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
Parametry
- trimChars
- ReadOnlySpan<Char>
Zwraca
Dotyczy
TrimStart()
- Źródło:
- String.Manipulation.cs
- Źródło:
- String.Manipulation.cs
- Źródło:
- String.Manipulation.cs
Usuwa wszystkie wiodące znaki odstępu z bieżącego ciągu.
public:
System::String ^ TrimStart();
public string TrimStart ();
member this.TrimStart : unit -> string
Public Function TrimStart () As String
Zwraca
Ciąg, który pozostaje po usunięciu wszystkich znaków odstępu od początku bieżącego ciągu. Jeśli nie można przycinać znaków z bieżącego wystąpienia, metoda zwraca bieżące wystąpienie bez zmian.
Uwagi
Metoda TrimStart
usuwa z bieżącego ciągu wszystkie wiodące znaki odstępu. Operacja przycinania zatrzymuje się, gdy napotkany jest znak bez odstępu. Jeśli na przykład bieżący ciąg to " abc xyz", TrimStart
metoda zwraca wartość "abc xyz".
Uwaga
TrimStart
Jeśli metoda usunie wszystkie znaki z bieżącego wystąpienia, ta metoda nie modyfikuje wartości bieżącego wystąpienia. Zamiast tego zwraca nowy ciąg, w którym zostaną usunięte wszystkie wiodące znaki odstępu znajdujące się w bieżącym wystąpieniu.
Dotyczy
TrimStart(Char)
- Źródło:
- String.Manipulation.cs
- Źródło:
- String.Manipulation.cs
- Źródło:
- String.Manipulation.cs
Usuwa wszystkie wiodące wystąpienia określonego znaku z bieżącego ciągu.
public:
System::String ^ TrimStart(char trimChar);
public string TrimStart (char trimChar);
member this.TrimStart : char -> string
Public Function TrimStart (trimChar As Char) As String
Parametry
- trimChar
- Char
Znak Unicode do usunięcia.
Zwraca
Ciąg, który pozostaje po wszystkich wystąpieniach trimChar
znaku, zostanie usunięty z początku bieżącego ciągu. Jeśli nie można przycinać znaków z bieżącego wystąpienia, metoda zwraca bieżące wystąpienie bez zmian.
Uwagi
Metoda TrimStart(System.Char)
usuwa z bieżącego ciągu wszystkie znaki wiodące trimChar
. Operacja przycinania zatrzymuje się, gdy napotkano znak, który nie trimChar
jest napotkany. Jeśli na przykład trimChar
jest i -
bieżący ciąg to "---abc---xyz----", TrimStart(System.Char)
metoda zwraca wartość "abc---xyz----".
Uwaga
TrimStart(System.Char)
Jeśli metoda usunie wszystkie znaki z bieżącego wystąpienia, ta metoda nie modyfikuje wartości bieżącego wystąpienia. Zamiast tego zwraca nowy ciąg, w którym zostaną usunięte wszystkie znaki wiodące trimChar
znalezione w bieżącym wystąpieniu.
Dotyczy
TrimStart(Char[])
- Źródło:
- String.Manipulation.cs
- Źródło:
- String.Manipulation.cs
- Źródło:
- String.Manipulation.cs
Usuwa wszystkie wiodące wystąpienia zestawu znaków określonych w tablicy z bieżącego ciągu.
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
Parametry
- trimChars
- Char[]
Tablica znaków Unicode do usunięcia lub null
.
Zwraca
Ciąg, który pozostaje po wszystkich wystąpieniach znaków w parametrze trimChars
, zostanie usunięty z początku bieżącego ciągu. Jeśli trimChars
jest lub null
pusta tablica, zamiast tego zostaną usunięte znaki odstępu. Jeśli nie można przycinać znaków z bieżącego wystąpienia, metoda zwraca bieżące wystąpienie bez zmian.
Przykłady
W poniższym przykładzie przedstawiono podstawową funkcjonalność TrimStart metody:
// 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
W poniższym przykładzie użyto TrimStart metody do przycinania białych znaków i komentarzy z wierszy kodu źródłowego. Metoda StripComments
opakowuje wywołanie i przekazuje ją do TrimStart tablicy znaków, która zawiera spację i znak komentarza, który jest apostrofem ( ' ) w Visual Basic i ukośnikiem ( / ) w języku C# lub F#. Metoda jest również wywoływana TrimStart w celu usunięcia wiodącego odstępu podczas oceniania, czy ciąg jest komentarzem.
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
Poniższy przykład ilustruje wywołanie StripComments
metody .
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.
Uwagi
Metoda TrimStart(System.Char[])
usuwa z bieżącego ciągu wszystkie znaki wiodące, które znajdują się w parametrze trimChars
. Operacja przycinania zatrzymuje się, gdy napotkano znak, który nie znajduje się.trimChars
Jeśli na przykład bieżący ciąg to "123abc456xyz789" i trimChars
zawiera cyfry z "1" do "9", TrimStart(System.Char[])
metoda zwraca wartość "abc456xyz789".
Uwaga
TrimStart(System.Char[])
Jeśli metoda usunie wszystkie znaki z bieżącego wystąpienia, ta metoda nie modyfikuje wartości bieżącego wystąpienia. Zamiast tego zwraca nowy ciąg, w którym zostaną usunięte wszystkie znaki wiodące znajdujące się w trimChars
parametrze znajdującym się w bieżącym wystąpieniu.
Uwagi dotyczące wywoływania
.NET Framework 3.5 SP1 i starsze wersje utrzymują wewnętrzną listę znaków odstępów, które ta metoda przycina, jeśli trimChars
jest lub jest null
pusta tablica. Począwszy od .NET Framework 4, jeśli trimChars
jest lub jest null
pustą tablicą, metoda przycina wszystkie znaki Unicode odstępami (czyli znaki, które generują wartość zwracaną true
podczas przekazywania do IsWhiteSpace(Char) metody). Ze względu na tę zmianę Trim() metoda w .NET Framework 3.5 SP1 i wcześniejszych wersjach usuwa dwa znaki, ZERO WIDTH SPACE (U+200B) i ZERO WIDTH NO-BREAK SPACE (U+FEFF), że Trim() metoda w .NET Framework 4 i nowszych wersjach nie usuwa. Ponadto Trim() metoda w .NET Framework 3.5 z dodatkiem SP1 i wcześniejszych wersjach nie przycina trzech znaków odstępów Unicode: MONGOLSKI SEPARATOR WEL (U+180E), WĄSKIE MIEJSCE BEZ PRZERWY (U+202F) i ŚREDNIA PRZESTRZEŃ MATEMATYCZNA (U+205F).
Zobacz też
Dotyczy
Opinia
https://aka.ms/ContentUserFeedback.
Dostępne już wkrótce: W 2024 r. będziemy stopniowo wycofywać zgłoszenia z serwisu GitHub jako mechanizm przesyłania opinii na temat zawartości i zastępować go nowym systemem opinii. Aby uzyskać więcej informacji, sprawdź:Prześlij i wyświetl opinię dla