String.TrimStart Methode

Definitie

Overloads

Name Description
TrimStart()

Hiermee verwijdert u alle voorloopspaties uit de huidige tekenreeks.

TrimStart(Char)

Hiermee verwijdert u alle voorloop-exemplaren van een opgegeven teken uit de huidige tekenreeks.

TrimStart(Char[])

Hiermee verwijdert u alle voorloopvallen van een set tekens die zijn opgegeven in een matrix uit de huidige tekenreeks.

TrimStart()

Hiermee verwijdert u alle voorloopspaties uit de huidige tekenreeks.

public:
 System::String ^ TrimStart();
public string TrimStart();
member this.TrimStart : unit -> string
Public Function TrimStart () As String

Retouren

De tekenreeks die na alle spatietekens blijft, wordt verwijderd uit het begin van de huidige tekenreeks. Als er geen tekens uit het huidige exemplaar kunnen worden bijgesneden, retourneert de methode het huidige exemplaar ongewijzigd.

Opmerkingen

De TrimStart methode verwijdert alle voorloopspaties uit de huidige tekenreeks. De trimbewerking stopt wanneer er een niet-witruimteteken wordt aangetroffen. Als de huidige tekenreeks bijvoorbeeld 'abc xyz' is, retourneert de TrimStart methode 'abc xyz'.

Note

Als met de TrimStart methode tekens uit het huidige exemplaar worden verwijderd, wijzigt deze methode de waarde van het huidige exemplaar niet. In plaats daarvan wordt een nieuwe tekenreeks geretourneerd waarin alle voorloopspaties die in het huidige exemplaar worden gevonden, worden verwijderd.

Van toepassing op

TrimStart(Char)

Hiermee verwijdert u alle voorloop-exemplaren van een opgegeven teken uit de huidige tekenreeks.

public:
 System::String ^ TrimStart(char trimChar);
public string TrimStart(char trimChar);
member this.TrimStart : char -> string
Public Function TrimStart (trimChar As Char) As String

Parameters

trimChar
Char

Het Unicode-teken dat u wilt verwijderen.

Retouren

De tekenreeks die na alle exemplaren van het trimChar teken blijft, wordt verwijderd uit het begin van de huidige tekenreeks. Als er geen tekens uit het huidige exemplaar kunnen worden bijgesneden, retourneert de methode het huidige exemplaar ongewijzigd.

Opmerkingen

De TrimStart(System.Char) methode verwijdert alle voorlooptekens trimChar uit de huidige tekenreeks. De trimbewerking stopt wanneer er geen teken wordt trimChar aangetroffen. Als dit bijvoorbeeld trimChar het - resultaat is en de huidige tekenreeks '---abc---xyz----' is, retourneert de TrimStart(System.Char) methode 'abc---xyz----'.

Note

Als met de TrimStart(System.Char) methode tekens uit het huidige exemplaar worden verwijderd, wijzigt deze methode de waarde van het huidige exemplaar niet. In plaats daarvan wordt een nieuwe tekenreeks geretourneerd waarin alle voorlooptekens trimChar in het huidige exemplaar worden verwijderd.

Van toepassing op

TrimStart(Char[])

Hiermee verwijdert u alle voorloopvallen van een set tekens die zijn opgegeven in een matrix uit de huidige tekenreeks.

public:
 System::String ^ TrimStart(... cli::array <char> ^ trimChars);
public string TrimStart(params char[] trimChars);
member this.TrimStart : char[] -> string
Public Function TrimStart (ParamArray trimChars As Char()) As String

Parameters

trimChars
Char[]

Een matrix van Unicode-tekens die moeten worden verwijderd, of null.

Retouren

De tekenreeks die na alle exemplaren van tekens in de trimChars parameter blijft, wordt verwijderd uit het begin van de huidige tekenreeks. Als trimChars dit een of een lege matrix is null , worden in plaats daarvan spatietekens verwijderd. Als er geen tekens uit het huidige exemplaar kunnen worden bijgesneden, retourneert de methode het huidige exemplaar ongewijzigd.

Voorbeelden

In het volgende voorbeeld ziet u de basisfunctionaliteit van de TrimStart methode:

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

In het volgende voorbeeld wordt de TrimStart methode gebruikt om witruimte en commentaartekens uit regels met broncode te knippen. De methode StripComments verpakt een aanroep naar TrimStart en geeft deze door aan een tekenmatrix die een spatie en het opmerkingteken bevat. Dit is een apostrof (' ) in Visual Basic en een slash (/) in C# of F#. De TrimStart methode wordt ook aangeroepen om voorloopspaties te verwijderen bij het evalueren of een tekenreeks een opmerking is.

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

In het volgende voorbeeld ziet u vervolgens een aanroep naar de StripComments methode.

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.

Opmerkingen

De TrimStart(System.Char[]) methode verwijdert uit de huidige tekenreeks alle voorlooptekens die zich in de trimChars parameter bevinden. De trimbewerking stopt wanneer er een teken wordt aangetroffen dat zich niet bevindt trimChars . Als de huidige tekenreeks bijvoorbeeld '123abc456xyz789' is en trimChars de cijfers van '1' tot en met '9' bevat, retourneert de TrimStart(System.Char[]) methode 'abc456xyz789'.

Note

Als met de TrimStart(System.Char[]) methode tekens uit het huidige exemplaar worden verwijderd, wijzigt deze methode de waarde van het huidige exemplaar niet. In plaats daarvan wordt een nieuwe tekenreeks geretourneerd waarin alle voorlooptekens die zich in de trimChars parameter in het huidige exemplaar bevinden, worden verwijderd.

Notities voor bellers

De .NET Framework 3.5 SP1 en eerdere versies onderhouden een interne lijst met spatietekens die met deze methode worden bijgehouden als trimChars is null of een lege matrix. Beginnend met het .NET Framework 4, als trimCharsnull of een lege matrix is, worden met de methode alle Unicode-spatietekens (dat wil gezegd, tekens die een true retourwaarde opleveren wanneer ze worden doorgegeven aan de methode IsWhiteSpace(Char)). Vanwege deze wijziging verwijdert de methode Trim() in het .NET Framework 3.5 SP1 en eerdere versies twee tekens, ZERO WIDTH SPACE (U+200B) en ZERO WIDTH NO-BREAK SPACE (U+FEFF), dat de methode Trim() in de .NET Framework 4- en latere versies niet wordt verwijderd. Bovendien worden met de methode Trim() in het .NET Framework 3.5 SP1 en eerdere versies niet drie Unicode-witruimtetekens geknipt: MONGOOLSE KLINKERSCHEIDingsteken (U+180E), NARROW NO-BREAK SPACE (U+202F) en MEDIUM WISKUNDIGE RUIMTE (U+205F).

Zie ook

Van toepassing op