How to reformat a string to exclude leading zeros?

I have lines like this:

var abc = "002"; var def = "023"; 

How can I easily change the lines so that the leading zeros are reset?

+7
source share
6 answers

The easiest way:

 int.Parse(s).ToString(); 

All cropping methods do not work for inputs "0000", they return an empty string instead of the correct "0".

+14
source

Take a look at TrimStart :

 numberString = numberString.TrimStart('0'); 

From MSDN:

The TrimStart method removes all leading characters that are in the trimChars parameter from the current line. The trim operation stops when a character is found that is not in trimChars.

+19
source

If it is always with int, you can simply parse it:

 abc = int.Parse(abc).ToString() 
+2
source

var str = int.Parse(abc).ToString(); should do the job, I think. Convert the number to int, and then just convert it to a string.

+1
source
 var abc = "0023"; var zeroless = abc.TrimStart('0'); 

output: "23"

+1
source
  string some_string = "000045"; string ur_desire = int.Parse(some_string).ToString(); Console.WriteLine(ur_desire); 

thix ix is ​​a good answer, I think, because it also works with a negative number.

+1
source

All Articles