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

String.Format with a variable field width

MC
In C, printf allows us to specify a runtime (non-constant) field width
of a formatted value by using the * character. Is there something
like that for System.String.Format? In C#, I find myself having to
use concatenation, which is awkward to write and to read:

s.Format("{0," + fieldWidth.ToString() + "}", val);

Preferably, I'd like to nest two placeholders, like this:

s.Format("{0,{1}}", val, fieldWidth);

but it doesn't like that (Framework 2.0).

Is currently available somehow? I didn't see it in MSDN. If not, was
there never any noise about the lack of it in Framework 1.x?? And are
there any plans to add it in the near future?

Thanks,
Craig
Apr 29 '06 #1
4 3727
Use
string.format("{0}{1}", myField1, myField2);

Buddy Home
MCSD.Net

"MC" <no****@nowhere.com> wrote in message
news:6m********************************@4ax.com...
In C, printf allows us to specify a runtime (non-constant) field width
of a formatted value by using the * character. Is there something
like that for System.String.Format? In C#, I find myself having to
use concatenation, which is awkward to write and to read:

s.Format("{0," + fieldWidth.ToString() + "}", val);

Preferably, I'd like to nest two placeholders, like this:

s.Format("{0,{1}}", val, fieldWidth);

but it doesn't like that (Framework 2.0).

Is currently available somehow? I didn't see it in MSDN. If not, was
there never any noise about the lack of it in Framework 1.x?? And are
there any plans to add it in the near future?

Thanks,
Craig

Apr 29 '06 #2
MC
Buddy,

Thanks for replying but I really don't think that will do what I want.
In my example, I want to output just one value, formatted with an
"alignment" value that is specified by a variable instead of by a
constant. I believe your snippet will output two values with no
alignment specified, no?

Craig

On Sat, 29 Apr 2006 15:31:23 +0100, "Buddy Home" <Bu*******@Home.com>
wrote:
Use
string.format("{0}{1}", myField1, myField2);

Buddy Home
MCSD.Net

"MC" <no****@nowhere.com> wrote in message
news:6m********************************@4ax.com.. .
In C, printf allows us to specify a runtime (non-constant) field width
of a formatted value by using the * character. Is there something
like that for System.String.Format? In C#, I find myself having to
use concatenation, which is awkward to write and to read:

s.Format("{0," + fieldWidth.ToString() + "}", val);

Preferably, I'd like to nest two placeholders, like this:

s.Format("{0,{1}}", val, fieldWidth);

but it doesn't like that (Framework 2.0).

Is currently available somehow? I didn't see it in MSDN. If not, was
there never any noise about the lack of it in Framework 1.x?? And are
there any plans to add it in the near future?

Thanks,
Craig


Apr 30 '06 #3
You have to build the string to pass to the Format function, resulting in a
rather messy:

Console.WriteLine(string.Format(string.Format("{{0 ,{0}}}",wid),val));

Double braces are ignored by the format function, so if wid is 9 then this
gives {0,9}.

I would simply use concatenation, as it has much lower overhead and is far
easier to read :)

Cheers,
Jason

On Sun, 30 Apr 2006 05:31:15 GMT, MC wrote:
Buddy,

Thanks for replying but I really don't think that will do what I want.
In my example, I want to output just one value, formatted with an
"alignment" value that is specified by a variable instead of by a
constant. I believe your snippet will output two values with no
alignment specified, no?

Craig

On Sat, 29 Apr 2006 15:31:23 +0100, "Buddy Home" <Bu*******@Home.com>
wrote:
Use
string.format("{0}{1}", myField1, myField2);

Buddy Home
MCSD.Net

"MC" <no****@nowhere.com> wrote in message
news:6m********************************@4ax.com. ..
In C, printf allows us to specify a runtime (non-constant) field width
of a formatted value by using the * character. Is there something
like that for System.String.Format? In C#, I find myself having to
use concatenation, which is awkward to write and to read:

s.Format("{0," + fieldWidth.ToString() + "}", val);

Preferably, I'd like to nest two placeholders, like this:

s.Format("{0,{1}}", val, fieldWidth);

but it doesn't like that (Framework 2.0).

Is currently available somehow? I didn't see it in MSDN. If not, was
there never any noise about the lack of it in Framework 1.x?? And are
there any plans to add it in the near future?

Thanks,
Craig

Apr 30 '06 #4
MC
Jason,

Yes, I agree, that's nasty. I suppose I could use
..ToString(formatString) and .PadLeft or .PadRight to get the same
result as with placeholders, and I could even use StringBuilder to be
even more efficient.

Something like:

StringBuilder sb = new StringBuilder();
sb.Append("Item".PadRight(width));
sb.Append("Cost\n");
sb.Append("=========================\n");
foreach(Item item in items)
{
sb.Append(item.Description.PadRight(width));
sb.Append(item.Cost.ToString("C"));
sb.Append("\n");
}

It's still verbose compared with the * in printf but it's readable.

Thanks,
Craig

On Sun, 30 Apr 2006 23:12:00 +1000, JasonS <ga****@sobell.net> wrote:
You have to build the string to pass to the Format function, resulting in a
rather messy:

Console.WriteLine(string.Format(string.Format("{{ 0,{0}}}",wid),val));

Double braces are ignored by the format function, so if wid is 9 then this
gives {0,9}.

I would simply use concatenation, as it has much lower overhead and is far
easier to read :)

Cheers,
Jason

On Sun, 30 Apr 2006 05:31:15 GMT, MC wrote:
Buddy,

Thanks for replying but I really don't think that will do what I want.
In my example, I want to output just one value, formatted with an
"alignment" value that is specified by a variable instead of by a
constant. I believe your snippet will output two values with no
alignment specified, no?

Craig

On Sat, 29 Apr 2006 15:31:23 +0100, "Buddy Home" <Bu*******@Home.com>
wrote:
Use
string.format("{0}{1}", myField1, myField2);

Buddy Home
MCSD.Net

"MC" <no****@nowhere.com> wrote in message
news:6m********************************@4ax.com ...
In C, printf allows us to specify a runtime (non-constant) field width
of a formatted value by using the * character. Is there something
like that for System.String.Format? In C#, I find myself having to
use concatenation, which is awkward to write and to read:

s.Format("{0," + fieldWidth.ToString() + "}", val);

Preferably, I'd like to nest two placeholders, like this:

s.Format("{0,{1}}", val, fieldWidth);

but it doesn't like that (Framework 2.0).

Is currently available somehow? I didn't see it in MSDN. If not, was
there never any noise about the lack of it in Framework 1.x?? And are
there any plans to add it in the near future?

Thanks,
Craig


Apr 30 '06 #5

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 ' ...
2
by: MC | last post by:
Is it possible to use String.Format and a format specifier in such a way to ensure that a string does not exceed the field width in which it is placed? Example: string strings = {"Hello...
2
by: L | last post by:
Hi there, Something like this in C++ String sTmp; for (int i = 5; i <=15; i++) { sTmp.format("%2s", i) }
20
by: MLH | last post by:
120 MyString = "How many copies of each letter do you need?" 150 MyVariant = InputBox(MyString, "How Many?", "3") If MyVariant = "2" Then MsgBox "MyVariant equals the string '2'" If...
4
by: Jack | last post by:
Hi, I have the following Access DDL statment from which I generated a access table: CREATE TABLE TEST1 (ID COUNTER, NAME TEXT(50), STORAGESPACEASSIGNED TEXT(50) )
1
by: Darin | last post by:
I am using the following connection string: junk = "Provider=Microsoft.Jet.OLEDB.4.0;" junk &= "Data Source=\data;" junk &= "Extended Properties=""text;HDR=No;FMT=Delimited""" And this works...
2
by: tstephan | last post by:
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);...
3
by: harry.viet | last post by:
I use the prototype library and create a class that builds a grid using some parameters as mentioned below. Everything is working but only the parameters for Number formatting var gridopts = {...
0
by: kelvin.koogan | last post by:
Does .NET have an equivalent to using * to specify the width of a field in printf? I.e. is there an equivalent to: printf(“%*s”, width, “String”) when using String.Format or similar? TIA,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.