Help formating string

G

Guest

I have a string with n number of characters. I'm trying to create another string which is exactly 12 characters having leadings zeros and the input string at the end

ex: string1 = "7344", I want to format string1 such that it results another string with string1 at the end and leading zeros in front of it like: "000000007344

so I try string2=String.Format(??????,string1

does anyone know the correct formatting expression

Thank
 
W

William Ryan eMVP

I think you'll need to cast it to an int first so

int x = string.Parse(string1);//Must be able to be parsed or exception will
be thrown.

MessageBox.Show(x.ToString("000000000000"));

This will cause it to be 12 long with the difference between the lengths
shown in 0's
Opa said:
I have a string with n number of characters. I'm trying to create another
string which is exactly 12 characters having leadings zeros and the input
string at the end.
ex: string1 = "7344", I want to format string1 such that it results
another string with string1 at the end and leading zeros in front of it
like: "000000007344"
 
B

Barry Burton

While using PadLeft, as others have suggested, is a more elegant solution,
you COULD use string.Format as follows:

string s1 = "7344";
string s2 = string.Format( "{0:000000000000}", int.Parse( s1 ));

The result would be the same...

Barry

Opa said:
I have a string with n number of characters. I'm trying to create another
string which is exactly 12 characters having leadings zeros and the input
string at the end.
ex: string1 = "7344", I want to format string1 such that it results
another string with string1 at the end and leading zeros in front of it
like: "000000007344"
 

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

Top