Variable length padding in String.Format

V

VRSki

Hello,

I understand a variable/constant can be aligned when dumped to a string like
this:

String.Format("|{0:-5}|", "abc");

This would produce a left-justified output like this: |abc |

The question is how to substitute -5 with a variable, so that formatting
could be more flexible. Something like this, maybe?

int paddingLength = -5;
String.Format("|{0:paddingLength}|", "abc"); // this obviously will not work

What would be the correct syntax?

Thanks,
VR
 
A

Arnshea

Hello,

I understand a variable/constant can be aligned when dumped to a string like
this:

String.Format("|{0:-5}|", "abc");

This would produce a left-justified output like this: |abc |

The question is how to substitute -5 with a variable, so that formatting
could be more flexible. Something like this, maybe?

int paddingLength = -5;
String.Format("|{0:paddingLength}|", "abc"); // this obviously will not work

What would be the correct syntax?

Thanks,
VR

String.Format("|{0:" + paddingLength + "}|", "abc");

Or you can use a StringBuilder to construct the fmt string if you're
doing it a lot...
 
V

VRSki

Arnshea,

Thank you for your post.

I am really currious though, how did you come accross this information. I
looked for a while and could not find anything in MSDN or on-line describing
this. What resource / link would you recommend to use for string formatting?

Thanks once again,
VR

Thanks for the post. If you don't mind me asking,
 
J

Jeffrey Tan[MSFT]

Hi VRSki,

Thank you for the feedback.

I am not sure if Arnshea's link meets your need. This link talks about the
"Composite Formatting" feature of .Net Framework. However, I suspect you
may want to understand why we can use variable in the String.Format()
method like the code Arnshea provided in the first reply.

Regarding this point, let's explain it briefly. As documented in MSDN, the
first parameter of String.Format() method is a string, so we only need to
construct a string for the first parameter. We may programmatically
construct the string by concatenating multiple sub-string together into a
format string. That's what "|{0:" + paddingLength + "}|" does. Note: in
this statement, the C# operator "+" is predefined for numeric and string
types. When one or both operands are of type string, "+" concatenates the
string representations of the operands. So the net result is a format
string which can be passed to the first parameter of String.Format. The
link below talks about the C# "+" operator:
"+ Operator (C# Reference)"
http://msdn2.microsoft.com/en-us/library/k1a63xkz(VS.80).aspx

Anyway, if your concern relies on the "Composite Formatting" feature of
String.Format, Arnshea's link should be a good explanation. Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi VR,

Have you reviewed our replies to you? Do they make sense to you? If you
still need any help or have any concern, please feel free to tell me,
thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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