I have strings like this:
开发者_如何学Pythonvar abc = "002";
var def = "023";
How can I easily change the strings so the leading zeros are dropped?
Take a look at TrimStart:
numberString = numberString.TrimStart('0');
From MSDN:
The TrimStart method removes from the current string all leading characters that are in the trimChars parameter. The trim operation stops when a character that is not in trimChars is encountered.
The easiest correct way is:
int.Parse(s).ToString();
The trim methods all fail for inputs of "0000", they'll return an empty string instead of the correct "0".
If this is always with int, you can just parse it:
abc = int.Parse(abc).ToString()
var str = int.Parse(abc).ToString();
should do the work I think. Convert number to int, and then just convert it back to string.
var abc = "0023";
var zeroless = abc.TrimStart('0');
output: "23"
string some_string = "000045";
string ur_desire = int.Parse(some_string).ToString();
Console.WriteLine(ur_desire);
thix ix the good answer i think because it also works with negative number..
精彩评论