473,624 Members | 2,642 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 8201
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(s tring.Format("F irst 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(s tring.Format("F irst 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
12463
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?.. in other words I want to change it's Format: Horizontal Alignment from Left To Right if the data passed in is an Integer, Currency, or DateTime.
4
6598
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? Basically, I want to format a number from an array to a fix length. I want to line up digits of numbers in a column with respect to their position. For instant, in Col1 which contains all integers they should align to the right and in Col2 all number...
7
2029
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 (http://www.windowsformsdatagridhelp.com/default.aspx?ID=4bfab32d-9cff-4f5c-ba95-49bb9074a8bc), but I get no alignment at all when calling the class. George Shephard's site, while imminently useful, does not have an anwer for this issue.
7
1966
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 and the header looks wrong squashed to the right. If I could even add a trailing space but it trims any trailing spaces off. Thanks
4
25914
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
2297
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 aligned on specific memory boundaries. Moreover, in many situations the cryptographic code does not itself have control over the memory alignment of its parameters so the best it can do is to detect if these aligmments are correct and act...
10
2273
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. - Code - Description - Amount
5
4392
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. - Code - Description - Amount
2
20154
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 switch back to "Left" aligned for remaining paragraphs, the text within the Word document remained Centered. I'm using: VS2008 w/ .NET Framework 3.5, Microsoft.Office.Interop.Word Version 12.0.0.0, and I'm creating the document as a 97-2003 document...
0
8246
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8685
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
8631
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
8341
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,...
1
6112
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2612
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
1489
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.