C# String Trim() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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

ParameterDescription
trimCharsOptional. The characters to remove from both ends. Empty or null means white space.
trimCharOptional. A single character to remove (newer .NET, .NET Core 2.0 and later).

Return Value

Return ValueDescription
stringA 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);
    }
}

How It Works

  1. Spaces on both sides. The phrase has 3 spaces in front and 2 behind.
  2. Brackets show the edges. The result fits tightly between [ and ].
  3. The inner space stays. The space between hello and world is in the middle, so it is kept.
  4. 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('*'));
    }
}

How It Works

  1. A set of two. Trim('-', '=') removes any mix of those two characters from both ends, in any order.
  2. Stops at a letter. Trimming moves inward until it meets T on the left and e on the right.
  3. Uneven ends are fine. **bold* has two stars in front and one behind. All of them are removed.
  4. 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() + "]");
    }
}

How It Works

  1. Hidden characters. Typed or pasted input often carries tabs and line breaks you cannot see.
  2. Raw comparison fails. The tab, spaces, \r, and \n are part of the string, so it is not equal to Sam.
  3. All white space counts. Trim() removes tabs and line-break characters as well as spaces, and the comparison becomes True.
  4. 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.

Next: TrimEnd()

Remove spaces (or chosen characters) from the end of a string only.

TrimEnd() 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.

8 people found this page helpful