Author Topic: String.Format and Non-Universal Character Widths  (Read 1260 times)

reeda1283

  • Guest
String.Format and Non-Universal Character Widths
« on: October 04, 2013, 12:47:27 PM »
Hi,
I'm trying to create a leaderboard with 3 "columns": standing, name, and score. Ideally, the standing appears first and is left justified, the name of the player with that standing is exactly 3 uniform-width spaces from the left (also left-justified), and then the score is right justified.

I am attempting to use string.format to do this, but even though I am setting the character widths of each column within string.format, shorter width characters (like "1") seem to be throwing the entire thing off. Could anyone take a look at my results, and then my code below and let me know if there is anything I can tweak to get the desired effect.

Thanks!



Code:
  1. int counter = 1;
  2.         foreach (float height in top5)
  3.         {
  4.         label.text +=
  5.                 string.Format("{0, -5}{1, -10}{2, 9}", counter.ToString()
  6.                 + ". ", "ME: ", string.Format("{0:00.00}", height)) + " m\n";
  7.         counter++;
  8.         }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: String.Format and Non-Universal Character Widths
« Reply #1 on: October 04, 2013, 08:52:52 PM »
Create 3 labels per row, not just 1. Unless the font is monospaced, you won't be able to get the spacing correct with just one label.