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

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...