In C#, TrimStart() removes characters from the beginning of a string only. The end is left exactly as it was. With no arguments it removes white space; with arguments it removes the characters you list.
Use it for things like leading zeros, indentation, or prefix symbols. Like Trim(), it never touches the middle, and it returns a new string.
This is an instance member of System.String. Call it on a string variable, such as text.TrimStart(...). Calling it on a null variable throws NullReferenceException. C# strings are immutable: TrimStart() never changes the original text. It gives back a new string (or the same one when there is nothing to change), so keep the result in a variable.
Syntax
Call it with no arguments for white space, or pass the characters to strip from the front.
C#
string a = " indent".TrimStart(); // "indent"
string b = "000420".TrimStart('0'); // "420"
string c = "// note".TrimStart('/', ' '); // "note"
Call
What it does
text.TrimStart()
Remove leading white space.
text.TrimStart('0')
Remove leading zeros.
text.TrimStart('/', ' ')
Remove any mix of slashes and spaces from the front.
If every character is in the trim set, the result is an empty string. On .NET Framework, TrimStart() with no arguments still works through the params char[] overload; newer .NET adds a dedicated no-argument version and a single-char version.
Library declarations
C#
public string TrimStart(); // newer .NET
public string TrimStart(char trimChar); // newer .NET
public string TrimStart(params char[]? trimChars);
Parameters
Parameter
Description
trimChars / trimChar
Optional. The characters to remove from the start. Empty or left out means white space.
Return Value
Return Value
Description
string
A new string without the leading characters, or the same text when nothing needed trimming.
C# Examples
Example 1 — Clean the Front and Keep the Back
Trim leading spaces and show that trailing spaces survive.
C#
using System;
class Program {
static void Main() {
Console.WriteLine("[" + " indent".TrimStart() + "]");
Console.WriteLine("[" + " a ".TrimStart() + "]");
}
}
Output
[indent]
[a ]
How It Works
Leading spaces go. The three spaces before indent are removed.
Trailing spaces stay. In " a " only the front is cleaned, so two spaces remain before the closing bracket.
One side only. This is the difference from Trim(), which would print [a].
Brackets as a ruler. Wrapping the result in [ ] is the easiest way to see spaces in console output.
Example 2 — Strip Leading Zeros (and Handle All Zeros)
Remove zeros from the front of a number string, and fix the case where nothing is left.