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.
string name = " Ana \t\n".Trim(); // "Ana" string path = "///usr/".Trim('/'); // "usr"
Try it
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.
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
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 ofaandbfrom the ends. It does not remove the text"ab". This surprises many people when trimming a file extension:"test.txt".TrimEnd(".txt".ToCharArray())givestes, becauset,xand.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
charargument. - 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.IsNullOrEmptyor compare to"".IsNullOrWhiteSpacedoes the trim check for you without allocating. - Padding is the inverse.
PadLeftandPadRightadd characters to reach a width. - Internal whitespace is not collapsed. Use
Regex.Replace(s, @"\s+", " ")orSplitwithRemoveEmptyEntriesfollowed byJointo normalize it.