473,508 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Alignment and string.Format

In the docs for String.Format there is an alignment component which
allows you to pad the field to a certain number of spaces.

e.g.
FormatFName = String.Format("First Name = |{0,10}|", myFName);

Pads myFName, width of 10 minimum

I am used to printf which allows a '*' to be used to specify a
variable to be used at runtime to specify the width of the column.

e.g.
printf("%*s", iColWidth, myFName);
Pads myFName, width of iColWidth minimum

Am I missing something or did we lose that power in C#? I would hate
to have to build my format string at runtime just to do this.

Aug 9 '07 #1
2 8194
ts******@gmail.com wrote:
In the docs for String.Format there is an alignment component which
allows you to pad the field to a certain number of spaces.

e.g.
FormatFName = String.Format("First Name = |{0,10}|", myFName);

Pads myFName, width of 10 minimum

I am used to printf which allows a '*' to be used to specify a
variable to be used at runtime to specify the width of the column.

e.g.
printf("%*s", iColWidth, myFName);
Pads myFName, width of iColWidth minimum

Am I missing something or did we lose that power in C#? I would hate
to have to build my format string at runtime just to do this.
As far as I know, that's the only way. Ex:

string myFName = "tom";
int iColWidth = 10;
string FormatFName = string.Format(string.Format("First Name =
|{{0,{0}}}|", iColWidth), myFName);

--
Tom Porterfield
Aug 9 '07 #2
On Aug 9, 3:38 pm, Tom Porterfield <tppor...@mvps.orgwrote:
tstep...@gmail.com wrote:
In the docs for String.Format there is an alignment component which
allows you to pad the field to a certain number of spaces.
e.g.
FormatFName = String.Format("First Name = |{0,10}|", myFName);
Pads myFName, width of 10 minimum
I am used to printf which allows a '*' to be used to specify a
variable to be used at runtime to specify the width of the column.
e.g.
printf("%*s", iColWidth, myFName);
Pads myFName, width of iColWidth minimum
Am I missing something or did we lose that power in C#? I would hate
to have to build my format string at runtime just to do this.

As far as I know, that's the only way. Ex:

string myFName = "tom";
int iColWidth = 10;
string FormatFName = string.Format(string.Format("First Name =
|{{0,{0}}}|", iColWidth), myFName);

--
Tom Porterfield
Thanks, that is exactly what I ended up doing - ugly code though. I
am surprised .NET lost functionality however - it would have been easy
to add this.

Aug 10 '07 #3

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

Similar topics

2
12448
by: blackdevil1979 | last post by:
Hello, Is there a way to format the data when it is passed into the Crystal Report(CR).. for example.. In the original table, a number may be left aligned, how to change it to right alignment?.....
4
6591
by: Sam | last post by:
Hi everyone I have a question regarding string format. If I want to write a set of numeric values from arrays to a text file with the "right alignment" format as below, can it be done?...
7
2024
by: Earl | last post by:
Any known fixes for the wacky right-alignment bug in the WinForms datagrid (VS2003)? I've tried Ken's workaround...
7
1965
by: Doug Bell | last post by:
Hi Does anyone know (or point me where I can find) how to set the alignment of a DataGrid Column Header different to the alignment of the column. I am trying to show some Right aligned columns...
4
25868
by: Samuel Shulman | last post by:
Hi For numeric columns I want to align it to the right How can I do that? Samuel
11
2273
by: Brian Gladman | last post by:
A lot of low level cryptographic code and some hardware cryptographic accelerators either fail completely or perform very poorly if their input, output and/or key storage areas in memory are not...
10
2249
by: Rahul | last post by:
Friends, My Problem is related to writing a text file from a database using .Net. in a particular string format. I have a dataset with a table, let suppose in this table there are three fields....
5
4389
by: Rahul | last post by:
Friends, My Problem is related to writing a text file from a database using .Net. in a particular string format. I have a dataset with a table, let suppose in this table there are three fields....
2
20117
by: uamusa | last post by:
I am Dynamically generating a proposal(report) in MS Word. By default the Paragraph Alignment is "Left". For the First 6 Paragraphs I set the Alignment to "Center", and then when attempting to...
0
7229
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
7333
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
7398
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...
0
7502
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
5057
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
4716
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
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1566
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 ...
0
428
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.