Difference between TrimStart() and TrimEnd() in C#


TrimStart() method removes all leading occurrences of a set of characters, whereas TrimEnd()removes all trailing occurrences of a set of characters.

TrimStart()

The TrimStart() method removes all leading occurrences of a set of characters specified in an array.

Let us see an example to remove all leading zeros −

Example

 Live Demo

using System;
class Program {
   static void Main() {

      String str ="0009678".TrimStart(new Char[] { '0' } );
      Console.WriteLine(str);
   }
}

Output

9678

TrimEnd()

The TrimEnd() method removes all trailing occurrences of a set of characters specified in an array.

Let us see an example to remove all trailing 1s −

Example

 Live Demo

using System;
class Program {
   static void Main() {

      String str ="7341111".TrimEnd(new Char[] { '1' } );
      Console.WriteLine(str);
   }
}

Output

734

Updated on: 22-Jun-2020

528 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements