yonan2236
yonan2236

Reputation: 13649

How to remove insignificant zeros from a string?

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 

Upvotes: 4

Views: 376

Answers (2)

Henrik
Henrik

Reputation: 23324

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

You might want to add

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

to avoid converting 00000000 to an empty string.

Upvotes: 12

Niklas
Niklas

Reputation: 13145

This should be pretty efficient

string str = "000001234";
str = str.TrimStart('0');

Upvotes: 12

Related Questions