473,231 Members | 1,969 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,231 software developers and data experts.

Control string and format specifiers

Udi
Hi,
I tried looking for it in the documentation but with no luck.
What's the equivalent C control strings in C# for the following:

"%3d" ? (space padding)
"%-3d" ? (left alignment)
"%+3d" ? (adding +/- sign)
"% 3d" ? (adding a space in case of positive vaklue)

(E.g - for zero padding - "%03d" is {0:d3}, but how do I do space
padding - "%3d"? )

I'll appreciate it if you can direct me to the relevant link.

Thanks!
Udi

Dec 6 '05 #1
6 7469
Udi wrote:
I tried looking for it in the documentation but with no luck.
What's the equivalent C control strings in C# for the following:

"%3d" ? (space padding)
"%-3d" ? (left alignment)
"%+3d" ? (adding +/- sign)
"% 3d" ? (adding a space in case of positive vaklue)

(E.g - for zero padding - "%03d" is {0:d3}, but how do I do space
padding - "%3d"? )

I'll appreciate it if you can direct me to the relevant link.


If you look up "formatting strings" on MSDN, you'll find the "general"
rules for formatting, which includes alignment. So for the example you
gave, you need {0,3:d} for right alignment, and {0,-3:d} for left
alignment. You can also mix and match: {0,3:d2} will give " 05" for
instance.

Jon

Dec 6 '05 #2
Udi
Thanks Jon, but this is the link I've looked at, however I didn't see
this notation {0,3:d2} anywhere.
Is there any equivalent for the "u" format specifier?
What if I have a 32 bit value that want to control the way it is
printed with the help of the format specifier just like in printf?
E.g. -
int i = -1;
printf("%u", i);

How do I do it in c#?
Thanks again for your time!
Udi.

Dec 6 '05 #3
Udi wrote:
Thanks Jon, but this is the link I've looked at, however I didn't see
this notation {0,3:d2} anywhere.
Well, it's the {index,alignment:formatString} notation - the d2 is the
formatString part, and the 3 is the alignment.
Is there any equivalent for the "u" format specifier?
What if I have a 32 bit value that want to control the way it is
printed with the help of the format specifier just like in printf?
E.g. -
int i = -1;
printf("%u", i);

How do I do it in c#?


Do you mean you want to treat a signed integer as an unsigned integer?
It's not clear to me (not having done any C for a while) what you're
trying to do.

Jon

Dec 6 '05 #4
Udi
yes, exactly.

say I have
int i = -1;

but I would like to print it as unsigned, meaning - in this case I'd
like WriteLine() to print the value of MAX_INT.
Is there a way to do it through the format specifiers? I.e. forcing the
value to be printed as unsigned?
Thanks!

Dec 6 '05 #5
Udi wrote:
yes, exactly.

say I have
int i = -1;

but I would like to print it as unsigned, meaning - in this case I'd
like WriteLine() to print the value of MAX_INT.
Is there a way to do it through the format specifiers? I.e. forcing the
value to be printed as unsigned?


Not that I know of, but why don't you just cast it to an unsigned int
in the first place?

Console.WriteLine ("{0:d}", (uint)i);

Jon

Dec 6 '05 #6
Udi
Thanks Jon.
That's what I had in mind, just wanted to see if there's another way of
doing it to save the casting.
Udi.

Dec 6 '05 #7

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

Similar topics

20
by: Pierre Fortin | last post by:
Hi! "Python Essential Reference" - 2nd Ed, on P. 47 states that a string format can include "*" for a field width (no restrictions noted); yet... >>> "%*d" % (6,2) # works as expected ' ...
0
by: Josiah Carlson | last post by:
Good day everyone, I have produced a patch against the latest CVS to add support for two new formatting characters in the struct module. It is currently an RFE, which I include a link to at the...
6
by: Servé Lau | last post by:
suppose I want to use sscanf get the functionname from a function prototype. Is the following format string correct then? char funcname; char *p = "func(void)"; sscanf(p, "%s", funcname);...
2
by: ramonred | last post by:
Hi, I am having trouble with a little piece of code that formats an SQL string. I am not looking for folks to debug my code, what I would like to know is how can I see the string that I've built...
1
by: Chris Morse | last post by:
Hi, I've been trying to figure out where in the documentation it describes all the String.Format() formatting specifiers. So far, I've been guessing and picking up specifiers in sample code.....
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
3
by: stathisgotsis | last post by:
Hello everyone, Trusting K&R2 i thought until recently that spaces are ignored in scanf's format string. Reading arguments to the contrary confused me a little. So i now ask: Is...
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?
1
by: (2b|!2b)==? | last post by:
I have the following line in my code, which is supposed to skip commas and white space and to read a comma seperated value string into the appropriate variables: sscanf(temp.c_str(),...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.