开发者

Format a string into columns

Is there a cool way to take something like this:

Customer Name - City, State - ID
Bob Whiley - Howesville, TN - 322
Marley Winchester - Old Towne, CA - 5653

and format it to something like this:

Customer Name     - City,       State - ID
Bob Whiley        - Howesville, TN    - 322
Marley Winchester - Old Towne,  CA    - 5653

Using string format commands?

I am not too hung up on what to do if one is very long. For example this would be ok by me:

Customer Name     - City,       State - ID
Bob Whiley        - Howesville, TN    - 322
Marley Winchester - Old Towne,  CA    - 5653
Super Town person - Long Town Name, WA- 45648 

To provide some context. I have a drop down box that shows info very similar to this. Right now my code to create the item in the drop down looks like this:

public partial class CustomerDataContract
{
    public string DropDownDisplay
    {
        get
        {
             return  Name + " - " + City + ",  " + State + " - " + ID;
        }
    }
}

I am looking for a way to format this better. Any ideas?


This is what I ended up with:

HttpContext.Current.Server.HtmlDecode(
    String.Format("{0,-27} - {1,-15}, {2, 2} - {3,5}", 
    Name, City, State, ID)
    .Replace(" ", " "));

The HtmlDecode changes the   to a space that can withstand the space removing formatting of the dropdown l开发者_开发技巧ist.


You can specify the number of columns occupied by the text as well as alignment using Console.WriteLine or using String.Format:

// Prints "--123       --"
Console.WriteLine("--{0,-10}--", 123);
// Prints "--       123--"
Console.WriteLine("--{0,10}--", 123);

The number specifies the number of columns you want to use and the sign specifies alignment (- for left alignment, + for right alignment). So, if you know the number of columns available, you could write for example something like this:

public string DropDownDisplay { 
  get { 
    return String.Format("{0,-10} - {1,-10}, {2, 10} - {3,5}"),
      Name, City, State, ID);
  } 
} 

If you'd like to calculate the number of columns based on the entire list (e.g. the longest name), then you'll need to get that number in advance and pass it as a parameter to your DropDownDisplay - there is no way to do this automatically.


In addition to Tomas's answer I just want to point out that string interpolation can be used in C# 6 or newer.

// with string format
var columnHeaders1 = string.Format($"|{0,-30}|{1,-4}|{2,-15}|{3,-30}|{4,-30}|{5,-30}|{6,-30}", "ColumnA", "ColumnB", "ColumnC", "ColumnD", "ColumnE", "ColumnF", "ColumnG");

// with string interpolation
var columnHeaders2 = $"|{"ColumnA",-30}|{"ColumnB",-4}|{"ColumnC",-15}|{"ColumnD",-30}|{"ColumnE",-30}|{"ColumnF",-30}|{"ColumnG",-30}";


I am unable to add a comment above, but in the accepted answer it was stated:

If you'd like to calculate the number of columns based on the entire list (e.g. the longest name), then you'll need to get that number in advance and pass it as a parameter to your DropDownDisplay - there is no way to do this automatically.

This can in fact be done programmatically at runtime by creating the format string 'on the fly':

string p0 = "first";
string p1 = "separated by alignment value x";

int x = n * 10; // calculate the alignment x as needed

// now use x to give something like: {0,-20}, {1}
string fmt = "{0,-" + x + "},{1}"; // or whatever formatting expression you want

// then use the fmt string
string str = string.Format(fmt, p0, p1)

// with n = 2 this would give us
"first               ,separated by alignment value x"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜