Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all zeros from string's beginning?

Tags:

string

c#

I have a string which is beginning with zeros:

string s = "000045zxxcC648700"; 

How can I remove them so that string will look like:

string s = "45zxxcC648700"; 
like image 529
hsz Avatar asked Apr 27 '10 18:04

hsz


People also ask

How do you remove all zeros from a string?

The replaceAll() method of the String class accepts two strings representing a regular expression and a replacement String and replaces the matched values with given String. The ^0+(?! $)"; To remove the leading zeros from a string pass this as first parameter and “” as second parameter.

How can I remove zeros in front of a number in SQL?

Basically it performs three steps: Replace each 0 with a space – REPLACE([CustomerKey], '0', ' ') Use the LTRIM string function to trim leading spaces – LTRIM(<Step #1 Result>) Lastly, replace all spaces back to 0 – REPLACE(<Step #2 Result>, ' ', '0')


2 Answers

I would use TrimStart

string no_start_zeros = s.TrimStart('0'); 
like image 86
SwDevMan81 Avatar answered Sep 21 '22 10:09

SwDevMan81


You can use .TrimStart() like this:

s.TrimStart('0') 

Example:

string s = "000045zxxcC648700"; s = s.TrimStart('0'); //s == "45zxxcC648700" 
like image 36
Nick Craver Avatar answered Sep 20 '22 10:09

Nick Craver