Windows Develop Bookmark and Share   
 index > Windows Forms General > Remove Left Padding of "0"
 

Remove Left Padding of "0"

THis is going to seem very elementary but is there an easy way to remove padding from a string that is LeftPadded with "0"?


I have for example: "4422".PadLeft(8,char.Parse("0"));

Gives me "00004422".

I thought that string.LastIndexof("0",0) would give me 3 but instead it gives me 0. I found out that LastIndexOf starts from the back working its way forward. THis will not work for me if a user enters something like 20.43. Basically I need to remove all the left padded "0"s. Does anyone have an easy way of doing this?
Santiago Perez
SantiagoPerez  Thursday, September 10, 2009 6:19 PM
Just found an easy fix with a function I've never used:

protected string RemovePadding(string value)

{

if (value.StartsWith("0"))

{

char[] charsToTrim = {'0'};

value = value.TrimStart(charsToTrim);

}

return value;

}


Santiago Perez
  • Marked As Answer bySantiagoPerez Thursday, September 10, 2009 6:32 PM
  •  
SantiagoPerez  Thursday, September 10, 2009 6:32 PM
Just found an easy fix with a function I've never used:

protected string RemovePadding(string value)

{

if (value.StartsWith("0"))

{

char[] charsToTrim = {'0'};

value = value.TrimStart(charsToTrim);

}

return value;

}


Santiago Perez
  • Marked As Answer bySantiagoPerez Thursday, September 10, 2009 6:32 PM
  •  
SantiagoPerez  Thursday, September 10, 2009 6:32 PM

You can use google to search for other answers

Custom Search

More Threads

• MDI, StatusStrip on Maximized Child Form.
• How does AutoScrolling work?
• Using configurationmanager in Windows form, vs.net 2.0
• Highlighting a PropertyGrid's SelectedGridItem value
• Change Focus after Move
• Running program on a website
• Better AutoComplete on Combobox
• ABout multithreading in windows applicattions
• Autoscroll
• Refer to controls trouhg Forms