How to remove leading zeros using C#

前端 未结 8 1491
忘掉有多难
忘掉有多难 2020-12-01 04:16

How to remove leading zeros in strings using C#?

For example in the following numbers, I would like to remove all the leading zeros.

0001234
00000012         


        
8条回答
  •  感动是毒
    2020-12-01 04:31

    TryParse works if your number is less than Int32.MaxValue. This also gives you the opportunity to handle badly formatted strings. Works the same for Int64.MaxValue and Int64.TryParse.

    int number;
    if(Int32.TryParse(nvarchar, out number))
    {
       // etc...
       number.ToString();
    }

提交回复
热议问题