In C#, Trim() removes characters from both ends of a string. With no arguments it removes all white space: spaces, tabs (\t), and line breaks (\r, \n). Characters in the middle are never removed.
You can also pass the characters you want stripped, such as Trim('-', '='). To clean only one side, use TrimStart() or TrimEnd(). The cleaned text comes back as a new string.
This is an instance member of System.String. Call it on a string variable, such as text.Trim(...). Calling it on a null variable throws NullReferenceException. C# strings are immutable: Trim() 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 list the characters to strip.
C#
string raw = " hello world ";
string clean = raw.Trim(); // "hello world"
string title = "--Title--".Trim('-'); // "Title"
Call
What it does
text.Trim()
Remove white space from both ends.
text.Trim('*')
Remove one chosen character from both ends (single-char overload in newer .NET).
text.Trim('-', '=')
Remove any of several characters from both ends.
input?.Trim()
Trim only when input is not null.
“White space” means every character for which char.IsWhiteSpace is true, including non-breaking spaces. When you pass characters, only those are removed; spaces are no longer trimmed unless you list ' ' too. Trimming stops at the first character that is not in the set.
Library declarations
C#
public string Trim();
public string Trim(char trimChar); // newer .NET
public string Trim(params char[]? trimChars);
Parameters
Parameter
Description
trimChars
Optional. The characters to remove from both ends. Empty or null means white space.
trimChar
Optional. A single character to remove (newer .NET, .NET Core 2.0 and later).
Return Value
Return Value
Description
string
A new string without the leading and trailing characters, or the same text when there was nothing to trim.
C# Examples
Example 1 — Trim Spaces from Both Ends
Clean a padded phrase and compare the lengths before and after.
C#
using System;
class Program {
static void Main() {
string raw = " hello world ";
string clean = raw.Trim();
Console.WriteLine("[" + clean + "]");
Console.WriteLine(raw.Length);
Console.WriteLine(clean.Length);
}
}
Output
[hello world]
16
11
How It Works
Spaces on both sides. The phrase has 3 spaces in front and 2 behind.
Brackets show the edges. The result fits tightly between [ and ].
The inner space stays. The space between hello and world is in the middle, so it is kept.
Five characters fewer. 16 minus the 5 edge spaces leaves 11, and raw still has all 16.
Example 2 — Trim Decorations Instead of Spaces
Strip - and = from a banner, and stars from a word.
C#
using System;
class Program {
static void Main() {
string banner = "--==Title==--";
Console.WriteLine(banner.Trim('-', '='));
string word = "**bold*";
Console.WriteLine(word.Trim('*'));
}
}
Output
Title
bold
How It Works
A set of two.Trim('-', '=') removes any mix of those two characters from both ends, in any order.
Stops at a letter. Trimming moves inward until it meets T on the left and e on the right.
Uneven ends are fine.**bold* has two stars in front and one behind. All of them are removed.
Spaces are not in the set. When you pass characters, only those are trimmed. Add ' ' to the list if spaces must go too.
Example 3 — Clean Typed Input Before Comparing
Input with a tab and a Windows line break fails an equality test until it is trimmed.
C#
using System;
class Program {
static void Main() {
string input = "\t Sam \r\n";
Console.WriteLine(input == "Sam");
Console.WriteLine(input.Trim() == "Sam");
Console.WriteLine("[" + input.Trim() + "]");
}
}
Output
False
True
[Sam]
How It Works
Hidden characters. Typed or pasted input often carries tabs and line breaks you cannot see.
Raw comparison fails. The tab, spaces, \r, and \n are part of the string, so it is not equal to Sam.
All white space counts.Trim() removes tabs and line-break characters as well as spaces, and the comparison becomes True.
Trim at the door. A good habit is to trim input once, right after reading it, before you store or compare it.
Frequently Asked Questions
It returns a new string with white space removed from both the start and the end. White space includes spaces, tabs, and line breaks.
No. Trim only touches the ends. Use Replace(" ", "") to remove every space.
Pass them: text.Trim('-', '*'). Only the characters you list are removed, from both ends.
Strings are immutable. Trim returns a new string, so write text = text.Trim();
Trim cleans both ends, TrimStart cleans only the beginning, and TrimEnd cleans only the end. All three accept optional characters to remove.