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

CsharpProgrammingServer Side Programming

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
raja
Published on 16-Aug-2018 15:33:56
Advertisements