C# String.TrimEnd() – Examples

String.TrimEnd() method is used to remove all the trailing white-space characters from the current string.

String.TrimEnd(Char) method is used to remove specified character from trailing edge of the current string.

String.TrimEnd(Char[]) method is used to remove all specified characters from trailing edge of the current string.

String.TrimEnd() does not modify the original string but returns a new String instance with the trailing white-space/specified characters removed from the given string value.

In this tutorial, we will learn about the syntax of C# String.TrimEnd(), C# String.TrimEnd(Char) and C# String.TrimEnd(Char[]) method, and learn how to use these method with the help of examples.

TrimEnd()

String.TrimEnd() removes all the trailing white-space characters from the current string.

ADVERTISEMENT

Syntax

The syntax of TrimEnd() method is

String.TrimEnd()

Return Value

This method returns String value.

Example 1 – TrimEnd()

In this example, we will take a string str with trailing white spaces. To remove these trailing white spaces, we will call TrimEnd() method on this string str. TrimEnd() returns a new String instance.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        String str = "abc  ";
        String result = str.TrimEnd();
        Console.WriteLine($"Original String     : \"{str}\"");
        Console.WriteLine($"Result of TrimEnd() : \"{result}\"");
    }
}

Output

Original String     : "abc  "
Result of TrimEnd() : "abc"

Example 2 – TrimEnd() – No Trailing Spaces

In this example, we will take a string str with no trailing white spaces. If we call TrimEnd() method on this string str, TrimEnd() returns the original String instance on which we are calling the method. In this case alone, TrimEnd() does not create a new String.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        String str = "abc";
        String result = str.TrimEnd();
        Console.WriteLine($"Original String     : \"{str}\"");
        Console.WriteLine($"Result of TrimEnd() : \"{result}\"");
    }
}

Output

Original String     : "abc"
Result of TrimEnd() : "abc"

Example 3 – TrimEnd() – Null String

In we call TrimEnd() method on a null String, then this method throws System.NullReferenceException.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        String str = null;
        String result = str.TrimEnd();
        Console.WriteLine($"Original String     : \"{str}\"");
        Console.WriteLine($"Result of TrimEnd() : \"{result}\"");
    }
}

Output

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at Example.Main(String[] args) in D:\workspace\csharp\HelloWorld\Program.cs:line 6

TrimEnd(Char)

String.TrimEnd() removes all the trailing occurrences of a character from the current string.

Syntax

The syntax of TrimEnd(Char) method is

String.TrimEnd(Char trimChar)

where

ParameterDescription
trimCharA Unicode character to remove from trailing edge of the String.

Return Value

This method returns String value.

Example 4 – TrimEnd(Char)

In this example, we will take a string, and trim the specified character if present at the trailing edge of this string using TrimEnd(Char) method.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        String str = "abcdeaaa";
        Char trimChar = 'a';
        String result = str.TrimEnd(trimChar);
        Console.WriteLine($"Original String     : \"{str}\"");
        Console.WriteLine($"Result of TrimEnd() : \"{result}\"");
    }
}

Output

Original String     : "abcdeaaa"
Result of TrimEnd() : "abcde"

Only the trailing edge is trimmed, not the leading edge.

TrimEnd(Char[])

String.TrimEnd() removes all the trailing occurrences of a set of characters specified in an array from the current string.

Syntax

The syntax of TrimEnd(Char[]) method is

String.TrimEnd(Char[] trimChars)

where

ParameterDescription
trimCharsThe array of Unicode characters to remove from trailing edge of given string.

Return Value

This method returns String value.

Example 5 – TrimEnd(Char[])

In this example, we will take a string and remove all the trailing characters that are in the specified character array trimChars, using TrimEnd(Char[]) method.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        String str = "abbcdeaafabab";
        Char[] trimChars = {'a', 'b'};
        String result = str.TrimEnd(trimChars);
        Console.WriteLine($"Original String     : \"{str}\"");
        Console.WriteLine($"Result of TrimEnd() : \"{result}\"");
    }
}

Output

Original String     : "abbcdeaafabab"
Result of TrimEnd() : "abbcdeaaf"

Conclusion

In this C# Tutorial, we have learnt the syntax of C# String.TrimEnd() method, and also learnt how to use this method with the help of C# example programs.