473,471 Members | 4,095 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

string.Format() Question

Hello Everyone,

Has anyone found a quick reference for the many string formatting
options for string.Format() ? I'm spending a ridiculous amount of time
trying to write one line of code that would take about 3 seconds in C.
(Sarcasm not intended.)

All I need is a format string to zero pad an integer out to 9 digits.

[C]
int n = 1234;
printf("PU%09", n); // Displays "PU000001234"

[C#]
int n = 1234;
string pu = string.Format("{0}", n); // How to pad to 9 digits?

If anyone has a link to the full format string syntax, let me
know--I'll probably run into a similar problem in the future. (MSDN
was no help.)

Many thanks,
Roger

Nov 16 '05 #1
7 18336
Roger Helliwell wrote:
[C#]
int n = 1234;
string pu = string.Format("{0}", n); // How to pad to 9 digits?


I think you would write:

string pu = string.Format("{0:n9}", n); // How to pad to 9 digits?

Bill
Nov 16 '05 #2
Roger Helliwell wrote:
(MSDN was no help.)


Also look here:
http://www.codeproject.com/csharp/custstrformat.asp

Bill
Nov 16 '05 #3
Roger Helliwell wrote:
(MSDN was no help.)


Another reference:
http://www.stevex.org/dottext/articles/158.aspx

Bill
Nov 16 '05 #4
On Sun, 20 Jun 2004 17:02:09 -0700 in
microsoft.public.dotnet.languages.csharp, "William"
<bi********@earthlink.net> wrote:
Also look here:
http://www.codeproject.com/csharp/custstrformat.asp


Thanks Bill... exactly what I was looking for!

Nov 16 '05 #5
Roger Helliwell wrote:
Thanks Bill... exactly what I was looking for!


You're welcome! I got lucky <g>.

Bill
Nov 16 '05 #6
Hi Roger,

Not really an answer to your question, but to format a DateTime you could the format characters on this page

http://msdn.microsoft.com/library/en...classtopic.asp

DateTime.Now.ToString("H:mm:ss:fff") gives you a 24-hour click with milliseconds.

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #7
Roger Helliwell wrote:
Hello Everyone,

Has anyone found a quick reference for the many string formatting
options for string.Format() ? I'm spending a ridiculous amount of time
trying to write one line of code that would take about 3 seconds in C.
(Sarcasm not intended.)

All I need is a format string to zero pad an integer out to 9 digits.

[C]
int n = 1234;
printf("PU%09", n); // Displays "PU000001234"

[C#]
int n = 1234;
string pu = string.Format("{0}", n); // How to pad to 9 digits?

If anyone has a link to the full format string syntax, let me
know--I'll probably run into a similar problem in the future. (MSDN
was no help.)


Information on formatting strings is more difficult to find that it
should be. For formatting numbers search the docs for "custom numeric
format strings".

--
mikeb
Nov 16 '05 #8

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...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
7
by: Tommy Vercetti | last post by:
The first three of these statements work. The fourth doesn't. String::Format("{0}{0}{0}{0}", S""); String::Format("{0}{1}{0}{0}", S"", S""); String::Format("{0}{1}{2}{0}", S"", S"", S"");...
38
by: nobody | last post by:
I know that given a FormatString and a DateTime you can use DateTime.ToString(...) to convert the DateTime to a String. My question is how can you turn that around? Given a String and a...
4
by: Chris Dunaway | last post by:
I have a table in the database with a phone number field. The phone number is stored without any punctuation (e. g. 9995551234). I wish to take that string and format it for display (e. g. (999)...
7
by: L. Scott M. | last post by:
Have a quick simple question: dim x as string x = "1234567890" ------------------------------------------------------- VB 6 dim y as string
5
by: AMP | last post by:
Hello, I want to add some variables to a string and this isnt working. textBox1.Text="'BSL version='+ bslVerHi+ bslVerLo"; What am I doing wrong? Thanks Mike
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.