Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Variable length padding in String.Format

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
VRSki - 19 Sep 2007 18:15 GMT
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
Arnshea - 19 Sep 2007 18:22 GMT
> Hello,
>
[quoted text clipped - 15 lines]
> 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...
VRSki - 19 Sep 2007 21:59 GMT
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,
>> Hello,
>>
[quoted text clipped - 22 lines]
> Or you can use a StringBuilder to construct the fmt string if you're
> doing it a lot...
Arnshea - 20 Sep 2007 02:01 GMT
> Arnshea,
>
[quoted text clipped - 35 lines]
>
> - Show quoted text -

See http://msdn2.microsoft.com/en-us/library/txafckwd(VS.71).aspx
Jeffrey Tan[MSFT] - 20 Sep 2007 03:33 GMT
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.
Jeffrey Tan[MSFT] - 24 Sep 2007 04:32 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.