473,883 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
+ 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 "PU00000123 4"

[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 18358
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.publi c.dotnet.langua ges.csharp, "William"
<bi********@ear thlink.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.To String("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 "PU00000123 4"

[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
4050
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 object); What happens if i will use String.Format("{0} and {1}", "one", "two") instead of String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0} and {1}", "one", "two")?
32
14941
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 ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
7
1644
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""); String::Format("{0}{1}{2}{3}", S"", S"", S"", S""); Question #1: Why? Question #2: What is the simplest way to get this to work. I would
38
807
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 FormatString, how can you convert the String back to a DateTime? DateTime.Parse(...) doesn't use the FormatString. Now admitedly, if the format string is just "MM", it can't be done. But if the format string is "yyyyMMdd", or "ddMMMyyyy hhmmsst", it...
4
2858
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) 555-1234). I know that I can use the .substring method of the string class to get the characters I want and format it: Dim s As String With strPhone
7
3122
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
2324
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
5247
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 that very same format string from an xml file: ....load xmlNode WorkNode...
0
9793
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11151
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10750
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10858
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9577
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7134
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5996
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4619
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4225
muto222
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.