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

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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...