String Formatting

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to format a string so it appears as columns in a ListBox. Of course if I use \t then the columns don't necessarily line up. Is there iomanip style functions for fomatting strings in C#?
 
can't you do a ToString override?

public override string ToString()
{
return String.Format("{0}'s birthday is on{1}.", name, birthday);
}



Eric said:
I need to format a string so it appears as columns in a ListBox. Of
course if I use \t then the columns don't necessarily line up. Is there
iomanip style functions for fomatting strings in C#?
 
Yeah, it is getting it from an overridden method but I need to know how to format the string. For example, here's how 2 records might format if I use \t. The columns don't line up

First Last Number MoreInf
VeryLongFirstName Last Number MoreInfo
 
Eric,

Since you have not specified how you get these values, or if thay can be
more than one work each, then I do not know if this will work. What you
take each of the strings (in effect each value for a "column") and pad it
with
trailing spaces using the String.PadRight() method.

Basiclly what you want to do is get each value (First, Last, Number etc) and
pad it to a given lenght (say each column should be 20 characters wide),
then
you could call it s.PadRight(20, ' '). Then you can build each item in the
list by
doing a contact (preferbly by using a StringBuilder for performance) each
padded string.

Hope this helps,

//Andreas

Eric said:
Yeah, it is getting it from an overridden method but I need to know how to
format the string. For example, here's how 2 records might format if I use
\t. The columns don't line up.
 
Note that this relies on using a font such as Courier New, which has equally spaced characters:

string.Format( "{0,-19}{1,-19}{2,-10}{3,-12}", list[i,0], list[i,1], list[i,2], list[i,3] )

First Last Number MoreInfo
VeryLongFirstName Last Number MoreInfo

Otherwise, you'll get something like this (Arial):

First Last Number MoreInfo
VeryLongFirstName Last Number MoreInfo

Chris A.R.
 
Hi Chris,

Thank you for improving upon my suggestion. I wasn't aware of the
formatting options you have indicated.

Perhaps you might have an answer to this:
There is the option of coding ToString(" ####.##")
Is there also someting like ToString("/ / ####.##")
to accomodate strings as well? I found no references in MDSN.

V.

Note that this relies on using a font such as Courier New, which has equally
spaced characters:

string.Format( "{0,-19}{1,-19}{2,-10}{3,-12}", list[i,0], list[i,1],
list[i,2], list[i,3] )

First Last Number MoreInfo
VeryLongFirstName Last Number MoreInfo

Otherwise, you'll get something like this (Arial):

First Last Number MoreInfo
VeryLongFirstName Last Number MoreInfo

Chris A.R.

Eric said:
Yeah, it is getting it from an overridden method but I need to know how to
format the string. For example, here's how 2 records might format if I use
\t. The columns don't line up.
 
Vanessa,

I'm not quite sure what you're asking for here. How about giving an example
of what you would like to start with and what you would like to finish with
in some pseudo-code and I'll see if I can answer your question.

Chris A.R.

Vanessa said:
Hi Chris,

Thank you for improving upon my suggestion. I wasn't aware of the
formatting options you have indicated.

Perhaps you might have an answer to this:
There is the option of coding ToString(" ####.##")
Is there also someting like ToString("/ / ####.##")
to accomodate strings as well? I found no references in MDSN.

V.

Note that this relies on using a font such as Courier New, which has equally
spaced characters:

string.Format( "{0,-19}{1,-19}{2,-10}{3,-12}", list[i,0], list[i,1],
list[i,2], list[i,3] )

First Last Number MoreInfo
VeryLongFirstName Last Number MoreInfo

Otherwise, you'll get something like this (Arial):

First Last Number MoreInfo
VeryLongFirstName Last Number MoreInfo

Chris A.R.

Eric said:
Yeah, it is getting it from an overridden method but I need to know how
to
format the string. For example, here's how 2 records might format if I use
\t. The columns don't line up.
First Last Number MoreInfo
VeryLongFirstName Last Number MoreInfo
 
a.ToString("The answer is ##,##.##")
this gets things lined up nicely.
++++++++++++++++++++++++++++++++
##,##.## is for numbers
I was wondering if there was a mask for strings
as wel and couldn't find a reference in MSDN
++++++++++++++++++++++++++++++++
Example:
private void Form1_Load(object sender, System.EventArgs e)
{
double a = 12345.345;
string b = "good";
textBox1.Text = a.ToString("The answer is ##,##.##")+String.Format(" This
result is {0,-9}.",b);

}
++++++++++++++++++++++++++++++++


Chris A. R. said:
Vanessa,

I'm not quite sure what you're asking for here. How about giving an example
of what you would like to start with and what you would like to finish with
in some pseudo-code and I'll see if I can answer your question.

Chris A.R.

Vanessa said:
Hi Chris,

Thank you for improving upon my suggestion. I wasn't aware of the
formatting options you have indicated.

Perhaps you might have an answer to this:
There is the option of coding ToString(" ####.##")
Is there also someting like ToString("/ / ####.##")
to accomodate strings as well? I found no references in MDSN.

V.

Note that this relies on using a font such as Courier New, which has equally
spaced characters:

string.Format( "{0,-19}{1,-19}{2,-10}{3,-12}", list[i,0], list[i,1],
list[i,2], list[i,3] )

First Last Number MoreInfo
VeryLongFirstName Last Number MoreInfo

Otherwise, you'll get something like this (Arial):

First Last Number MoreInfo
VeryLongFirstName Last Number MoreInfo

Chris A.R.

Eric said:
Yeah, it is getting it from an overridden method but I need to know
how
to
format the string. For example, here's how 2 records might format if I use
\t. The columns don't line up.
 
Back
Top