views:

117

answers:

2

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 list.

+4  A: 

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.

Tomas Petricek
Good answer! I think it works, but for some reason, when view the drop down, it is not working. I am guessing something is removing the extra spaces. :(
Vaccano
I need to find some way to insert   rather than a space.
Vaccano
if you need to use  , just do a string.Replace after the format. return String.Format("{0,-10} - {1,-10}, {2, 10} - {3,5}"), Name, City, State, ID).Replace(" ", " ");
Andy_Vulhop
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(" ", " "));
Vaccano
+1  A: 

I do not program in C# but if you have a function similar to printf that supports same specifiers you can easily do:

printf("%-20s - %-10, %-5s - %-5s", Name, City, State, ID);

the - specifier forces left alignment while the number between % and string specifier s ensures a maximum width that is padded with spaces if string is shorter.

This should be quite simple..

Jack