Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Strings and Text

C# String.Trim() methods

Reference for C# Trim(), TrimStart() and TrimEnd(): remove whitespace or chosen characters and prefixes from the ends of a string, with edge cases.

Trim returns a copy of a string without the leading and trailing characters you do not want. With no arguments it removes whitespace. TrimStart and TrimEnd work on one side only. The characters in the middle are never touched.

Typical uses are cleaning user input, file lines and CSV fields before comparing or parsing them.

C#
string name = "  Ana \t\n".Trim();   // "Ana"
string path = "///usr/".Trim('/');   // "usr"

Try it

C# Trimming whitespace and characters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Prefixes, suffixes and edge cases

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public string Trim()
public string Trim(char trimChar)
public string Trim(params char[]? trimChars)

public string TrimStart()
public string TrimStart(char trimChar)
public string TrimStart(params char[]? trimChars)

public string TrimEnd()
public string TrimEnd(char trimChar)
public string TrimEnd(params char[]? trimChars)
Name Description
trimChar A single character to remove from the chosen end(s).
trimChars A set of characters to remove. Passing null or an empty array means "whitespace".

There is no overload that removes a whole prefix or suffix string. Use EndsWith with slicing (see the example) for that.

Returns: a new string, or the same instance when there was nothing to trim.

Errors / undefined behavior: none. A string consisting only of removable characters becomes "". Calling on a null reference throws NullReferenceException as for any instance method.

Usage notes

  • Whitespace means Unicode white space. That includes the space, tab, newlines, the no-break space, and other separators. It is decided by char.IsWhiteSpace.
  • A set, not a sequence. Trim('a', 'b') removes any mix of a and b from the ends. It does not remove the text "ab". This surprises many people when trimming a file extension: "test.txt".TrimEnd(".txt".ToCharArray()) gives tes, because t, x and . are all in the set.
  • Trimming is by UTF-16 code unit. Characters outside the Basic Multilingual Plane are two chars and cannot be trimmed individually with a char argument.
  • Immutable. Use the return value. Nothing happens to the original.
  • Allocation-free trimming. On ReadOnlySpan<char>, Trim() returns a slice without copying. See spans and memory.
  • Validation pairs. After trimming, test with string.IsNullOrEmpty or compare to "". IsNullOrWhiteSpace does the trim check for you without allocating.
  • Padding is the inverse. PadLeft and PadRight add characters to reach a width.
  • Internal whitespace is not collapsed. Use Regex.Replace(s, @"\s+", " ") or Split with RemoveEmptyEntries followed by Join to normalize it.