C# String TrimStart() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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"
CallWhat 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

ParameterDescription
trimChars / trimCharOptional. The characters to remove from the start. Empty or left out means white space.

Return Value

Return ValueDescription
stringA 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() + "]");
    }
}

How It Works

  1. Leading spaces go. The three spaces before indent are removed.
  2. Trailing spaces stay. In " a " only the front is cleaned, so two spaces remain before the closing bracket.
  3. One side only. This is the difference from Trim(), which would print [a].
  4. 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.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine("000420".TrimStart('0'));

        string allZeros = "0000".TrimStart('0');
        Console.WriteLine("[" + allZeros + "]");

        string fixedValue = allZeros.Length == 0 ? "0" : allZeros;
        Console.WriteLine(fixedValue);
    }
}

How It Works

  1. Front zeros only. The three leading zeros are removed. The zero at the end of 420 is not at the start, so it stays.
  2. Everything matched. "0000" is made only of zeros, so the result is empty, shown as [].
  3. Put one zero back. The conditional operator returns "0" when the trimmed text is empty.
  4. Text, not numbers. This works on strings of any length, even ones too long for int or long.

Example 3 — Strip Comment Markers from Notes

Remove a mix of symbols and spaces from the front of note lines.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine("// note".TrimStart('/', ' '));
        Console.WriteLine("#  #tag".TrimStart('#', ' '));
        Console.WriteLine("x // y".TrimStart('/', ' '));
    }
}

How It Works

  1. Two kinds at once. Both slashes and the space are in the set, so all three leading characters are removed.
  2. Any order, any count. In # #tag the hash signs and spaces are mixed; trimming continues until it reaches t.
  3. Stops at the first outsider. x // y starts with x, which is not in the set, so nothing is removed at all.
  4. Not a search. TrimStart never looks past the first character that is not in the set. Use Replace or IndexOf for text in the middle.

Frequently Asked Questions

It returns a new string with white space, or the characters you pass, removed from the beginning only. The end of the string is not changed.
Use text.TrimStart('0'). If the result can be empty, for example for "000", replace an empty result with "0".
No. Use TrimEnd for the end, or Trim for both ends.
Yes. Pass them all: text.TrimStart('#', ' ', '/'). Any mix of those characters is removed from the front.
Yes. On .NET Framework it binds to TrimStart(params char[]) with an empty array, which trims white space. Newer .NET also has a dedicated overload.

Next: TryCopyTo()

Copy a string's characters into a Span<char> only if they fit; returns true or false instead of throwing.

TryCopyTo() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful