Cloud Training (Online)




How to Remove Leading Zeros from a String in C# ?


This blog post will explain how you can use the TrimStart function in C# to remove leading zeros from a string in C#.

Assume that you have a string with the leading zeros as shown below.

string input ="07777777777";

How to Remove Leading Zeros from a String in C# ?

You can use the TrimStart() function in C# and specify the trim characters as 0 in the first parameter.

TrimStart(new char[] { '0' })
using System;
class Program {
   static void Main() {
	  string input ="07777777777";
      string output = input.TrimStart(new char[] { '0' } );
      Console.WriteLine(output);
   }
}

You will see the output with the leading zeros stripped from the string.

7777777777