473,385 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

String Format

Jac
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
Nov 16 '05 #1
6 20503
string s = i.ToString("00000");

Jac wrote:
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


Nov 16 '05 #2
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.

"Jac" wrote:
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

Nov 16 '05 #3
Having re-read your post, you don't need to parse it back to an int.

"Jac" wrote:
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

Nov 16 '05 #4
Thanks, Brian. I wasn't aware of that one (but then I've only been
programming a few months).

"Brian Gideon" wrote:
string s = i.ToString("00000");

Jac wrote:
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


Nov 16 '05 #5
Jac,
In addition to:

string s = i.ToString("00000");

You could use:

string s = i.ToString("D5");
For details on Formatting in .NET see:

http://msdn.microsoft.com/library/de...ngoverview.asp

For standard numeric format strings (such as "D") see:

http://msdn.microsoft.com/library/de...matstrings.asp
Hope this helps
Jay

"Jac" <Ja*@discussions.microsoft.com> wrote in message
news:00**********************************@microsof t.com...
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

Nov 16 '05 #6
Jac wrote:
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#####");
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: san | last post by:
Hello, all! I have question about String.Format method. There are two variants: public static string Format(string, params object); and public static string Format(IFormatProvider, string, params...
5
by: raffelm | last post by:
I'm struggling to find a way to include long path names in a command line argument string that I have to build at runtime. I need to create a string like -o:"c:\my documents\my file.txt". ...
2
by: Bob | last post by:
I'm having trouble the string.Format() throwing exceptions and I can't figure out what I am doing wrong. Given the following setup code: string str = { "one", "two", "three", "four" }; double...
7
by: Alpha | last post by:
Hi, I'm maintaining C# code and am fairly new with C# programming. I'm looking for codes that's droping the 2nd digit of a nuber printed out and I suspect it's the code below. Can someone tell me...
6
by: Scewbedew | last post by:
Suppose I have the following code: string myFormat = "Line1/nLine 2"; string formattedString = string.Format(myFormat); ....that would produce a 2-line output as expected. But if I load...
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
4
by: carry | last post by:
Hi everybody, I need simple string formatting like string.Format() does, but just with strings what seems not to work. Can anyone tell me please why this doesn't work and how can it be done? This...
1
by: Remi THOMAS | last post by:
Hi, When you execute this line of code string script = string.Format("foreach (GroupDoc gdoc in {0}.SetGroup(\"{1}\") {\r\n", "p1", "p2"); You get System.FormatException was unhandled...
7
by: Rick | last post by:
With String.Format, if I have an incorrect number of args specified for a format string, compile fails. How can I implement similar design-time functionality for my own string functions?
8
by: Armando Rocha | last post by:
Hi, Hi have a string with 16 chars "25DD68EDEB8D5E11" and i want show it in form like this "25DD-68ED-EB8D-5E11", i try String.Format("{0:####-####-####-####}", mystr), but not work, i think...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.