C# Trim()

What is C# Trim method?

The Trim() method of strings is used to remove the leading and trailing spaces from the specified string.

You may use the trim() method in the following ways:

  • Without providing any arguments i.e. String.Trim()
string str = "     The string with whitespace    ".Trim();

This will remove unwanted whitespace from the left and right of the specified string.

  • Providing argument (See the example in the last section) i.e. Trim(Char[])

This way allows removing the specified leading and trailing character(s)

Besides, C# also has methods for removing the whitespace from the start or end of the specified string. These methods are:

  • TrimEnd()
  • TrimStart()

You may also remove the specified number of characters based on the index position in the string. The method for this purpose is:

  • Remove

See the examples below for learning how to remove leading and trailing spaces along with specifying other characters in the Trim() C# method.

The example of using Trim method

In the first example of the C# Trim() method, we will remove whitespace from the left and right of the string.

I intentionally added leading and trailing spaces in a string and then used the Trim() method as follows:

using System;

class String_trim_demo

{
    static void Main()
    {
        string str1 = "     The string with whitespace    ";
        string str2 = str1.Trim();

        System.Console.WriteLine(str1);

        System.Console.WriteLine("\nString after trim()\n");
        System.Console.WriteLine(str2);
        Console.ReadLine();
    }
}

c# trim

You can see the last string is displayed without whitespace.

Note: The original string remains the same after using the Trim() method, as such the string in C# is immutable.

An example of removing space from left/start

The following example uses the TrimStart() method for removing the spaces from the left side of the string only.

To show that only the spaces from the start are removed, I have concatenated another string to see spaces still exist on the right side of the string:

using System;

class String_trim_demo
{
    static void Main()
    {

        string str_Start = "     The string with whitespace    ";
        string str_Start2 = " Second String";
        string str_Start3 = str_Start.TrimStart();

        System.Console.WriteLine("Before TrimStart()\n");
        System.Console.WriteLine(str_Start + str_Start2);

        System.Console.WriteLine("\nString after TrimStart()\n");
        System.Console.WriteLine(str_Start3 + str_Start2);

        Console.ReadLine();
    }
}

c# trim_start

Removing trailing whitespace example

Similarly, you may use the TrimEnd() method for removing the spaces from the right side of the string. The example below shows how:

using System;

class String_trim_demo
{
    static void Main()

    {
        string str_End = "     The string with whitespace    ";
        string str_End2 = " Second String";
        string str_End3 = str_End.TrimEnd();

        System.Console.WriteLine("Before TrimEnd()\n");
        System.Console.WriteLine(str_End + str_End2);

        System.Console.WriteLine("\nString after TrimEnd()\n");
        System.Console.WriteLine(str_End3 + str_End2);

        Console.ReadLine();
    }
}

c# trim_end

Using the Trim method with multiple parameters

In the above examples, we only removed leading and trailing whitespace by using the Trim, TrimStart, and TrimEnd methods.

You may also use the Trim method for removing the other characters by providing parameters – as mentioned in the introductory section of this tutorial.

The general way of using the Trim with parameters:

Trim(Char[])

In the example below, we have specified an array of characters to be trimmed from the specified string. The Trim() method will remove any leading space, comma, forward slash, x, y, and z i.e.

char[] ArrTrim = {‘ ‘,’,’, ‘/’, ‘x’, ‘y’, ‘z’};

See the code and output below:

using System;

class String_trim_demo
{
    static void Main()
    {
        char[] ArrTrim = {' ',',', '/', 'x', 'y', 'z'};

        string Trim_multiple = "  xy C# cool yz ";
        Console.WriteLine("Original:" + Trim_multiple);
        Console.WriteLine("After Trim:" + Trim_multiple.Trim(ArrTrim));
        Console.ReadLine();
    }
}

The result:

Original:  xy C# cool yz

After Trim:C# Cool

Note that the Trimming will occur as long as a character in the parameter list is encountered in the specified string. As long as some other character is found, the trim function is terminated.
Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!