C# Tutorial C# Advanced C# References

C# String - TrimEnd() Method



The C# TrimEnd() method is used to removes all trailing instances of a character(s) / whitespaces from the current string.

Note: This method can be overloaded by passing different type of arguments to it.

Syntax

//removes whitespaces 
public string TrimEnd();

//removes specified character
public string TrimEnd(char trimChar);

//removes characters specified in the array
public string TrimEnd(params char[] trimChars);

Parameters

trimChar Specify the character to remove.
trimChars Specify the array of characters to remove.

Return Value

Returns the end side trimmed version of the string.

Exception

NA.

Example:

In the example below, TrimEnd() method returns the end side trimmed version of the string MyStr by removing whitespaces.

using System;

class MyProgram {
  static void Main(string[] args) {
    string MyStr = "    Hello World!.    ";
    string NewStr = MyStr.TrimEnd();

    Console.WriteLine(NewStr); 
  }
}

The output of the above code will be:

    Hello World!.

Example:

In the example below, TrimEnd() method returns the end side trimmed version of the string MyStr by removing the specified characters in the char array.

using System;

class MyProgram {
  static void Main(string[] args) {
    string MyStr = ",*#Hello World!#@!";
    char[] CharToTrim = {',','*','#','@','!'};
    string NewStr = MyStr.TrimEnd(CharToTrim);

    Console.WriteLine(NewStr); 
  }
}

The output of the above code will be:

,*#Hello World

❮ C# String Methods