String Format

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hey,

I have an int32 and a string.

The integer contains : 56
I want to fill up the string as follows : "00056"
The string must have a lenght of 5 en must be filled up with leading zero's.
How can I do this? I looked a little bit arround and don't find the way to
do this.

Thanks,
jac
 
Have you tried the PadLeft method of the String class?

Try:
int n = 56
string str = n.ToString();;
string str2 = str.PadLeft(5,'0');

where 5 is the number of characters in your new string and '0' is the
padding character. If you try it with str as "156", the result would be
"00156". It simply pads out the string to the length you require. You can
also PadRight to add digits or characters to the end of your string.

Don't forget to parse it before using it as an int again. I.E.

int n = Int32.Parse(str2);

Hope that helps.
 
Thanks, Brian. I wasn't aware of that one (but then I've only been
programming a few months).
 
Jac said:
Hey,

I have an int32 and a string.

The integer contains : 56
I want to fill up the string as follows : "00056"
The string must have a lenght of 5 en must be filled up with leading zero's.
How can I do this? I looked a little bit arround and don't find the way to
do this.

Thanks,
jac

int d = 56;
textBox1.Text = d.ToString("0#####");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

String formats 3
String to Int32? 5
Taking fields out of a string 2
List<String> 15
Change ToDictionary to ToList 1
Intersection of Lists 5
Trim String 9
about enum 3

Back
Top