Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove insignificant zeros from a string?

Tags:

c#

.net

I have the following string values.

00000062178221  
00000000054210  
00004210555001 

How can I cleanup the string and remove the zero paddings from the left? I'm using C# and .net 2.0. Expected results from above:

62178221  
54210  
4210555001 
like image 215
yonan2236 Avatar asked Nov 28 '22 09:11

yonan2236


2 Answers

This should be pretty efficient

string str = "000001234";
str = str.TrimStart('0');
like image 118
Niklas Avatar answered Dec 12 '22 11:12

Niklas


string s = "0001234";
s = s.TrimStart('0');

You might want to add

if (s == "") s = "0";

to avoid converting 00000000 to an empty string.

like image 44
Henrik Avatar answered Dec 12 '22 12:12

Henrik