Summary: in this tutorial, you will learn how to use the C# String TrimStart() method to remove all the leading white-space characters from a string.
Introduction to the C# String TrimStart() method
The String TrimStart() method allows you to remove all the leading white-space characters from a string. It returns a new string with the leading white spaces removed.
Here’s the syntax of the TrimStart() method:
public string TrimStart();Code language: C# (cs)The following example shows how to use the TrimStart() method to remove the leading white spaces from the string " Hello, World!";
using static System.Console;
var message = " Hello, World!";
var trimmed = message.TrimStart();
WriteLine(message);
WriteLine(trimmed);Code language: C# (cs)Output:
Hello, World!
Hello, World!Code language: C# (cs)In this example, the message string contains leading white spaces. We call the TrimStart() method on the message string. The method returns a result string (trimmed) that has the leading white spaces removed.
Note that the TrimStart() method doesn’t change the original string but returns a new string.
The Unicode standard defines white-space characters. The TrimStart() method removes any leading and trailing characters that, when passed to the Char.IsWhiteSpace() method, returns true:
public static bool IsWhiteSpace (char c);Code language: C# (cs)Using C# String TrimStart() to remove the leading instances of a character
The String TrimStart() method has an overload that allows you to remove the leading instances of a character from the current string:
public string TrimStart(char trimChar);Code language: C# (cs)In this syntax, the trimChar is a character to remove. The TrimStart() method returns a new string with the all the leading trimChar removed.
For example, the following program uses the TrimStart() method to remove the leading (/) from a string:
using static System.Console;
var comment = "//This is a in C#";
var result = comment.TrimStart('/');
WriteLine(comment);
WriteLine(result);Code language: C# (cs)Output:
//This is a comment in C#
This is a comment in C#Code language: C# (cs)Using C# String TrimStart() to remove the leading occurrences of a set of characters
The String TrimStart() method has an overload that removes any leading and trailing instances of specified characters from the current string:
public string TrimStart (
params char[]? trimChars
);Code language: C# (cs)In this syntax, the trimChars is an array of characters to remove. Note that if the trimChars is null, the TrimStart() method returns a new string with the leading white spaces removed.
The following example uses the TrimStart() method to remove the leading characters / and spaces from a string:
using static System.Console;
var comment = "// This is a comment in C#";
char[] trimChars = { '/', ' ' };
var result = comment.TrimStart(trimChars);
WriteLine(comment);
WriteLine(result);Code language: C# (cs)Output:
// This is a comment in C#
This is a comment in C#Code language: C# (cs)Summary
- Use the C# String
TrimStart()method to remove all the leading white spaces or a set of specified characters from a string.