How to remove leading zeros

How to remove leading zeros in datatype nvarchar using C #?

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

0001234 0000001234 00001234 
+116
string c #
Aug 10 '11 at 12:23
source share
6 answers

It really depends on how long the NVARCHAR is, since some of the above (especially those that convert via IntXX) methods will not work:

 String s = "005780327584329067506780657065786378061754654532164953264952469215462934562914562194562149516249516294563219437859043758430587066748932647329814687194673219673294677438907385032758065763278963247982360675680570678407806473296472036454612945621946"; 

Something like that

 String s ="0000058757843950000120465875468465874567456745674000004000".TrimStart(new Char[] { '0' } ); // s = "58757843950000120465875468465874567456745674000004000" 
+139
Aug 10 '11 at 12:27
source share

This is the code you need:

 string strInput = "0001234"; strInput.TrimStart('0'); 
+283
Aug 10 '11 at 12:32
source share

Code to avoid returning an empty string (when input looks like "00000").

 string myStr = "00012345"; myStr = myStr.TrimStart('0'); myStr = myStr.Length > 0 ? myStr : "0"; 
+27
Nov 19 '14 at 18:47
source share

return numberString.TrimStart('0');

+23
Aug 10 2018-11-12T00:
source share

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

 int number; if(Int32.TryParse(nvarchar, out number)) { // etc... number.ToString(); } 
+5
Aug 10 2018-11-12T00:
source share

using the following command, a single 0 will be returned when the input is 0.

string s = "0000000" s = int.Parse(s).ToString();

+2
Jul 10 '17 at 17:48
source share



All Articles