1

I'm generating a list of names and values. Unformatted it looks like this:

 - Atrox (Jesse Stone) - Level: 4
 - InternetInk (Benjamin Chiryoku) - Level: 4
 - Eliot_Ness (James Carter) - Level: 4
 - Kones (Dmitri Schukov) - Level: 4
 - 611er (Falk vanAlk) - Level: 3
 - Julian (Linus Torvalds) - Level: 2
 - Bot (Staat) - Level: 1
 - Hawke (Garen Hawke) - Level: 1

But I want it to look like this:

- Atrox (Jesse Stone)                   - Level: 4
- InternetInk (Benjamin Chiryoku)       - Level: 4
- Eliot_Ness (James Carter)             - Level: 4
- Kones (Dmitri Schukov)                - Level: 4
- 611er (Falk vanAlk)                   - Level: 3
- Julian (Linus Torvalds)               - Level: 2
- Bot (Staat)                           - Level: 1
- Hawke (Garen Hawke)                   - Level: 1

My line for writing is:

fileWriter.WriteLine(" - " + player.accountName + " (" + player.charName + ") - Level: " + player.level);

4

1 回答 1

3

Use String.PadRight() method:

public string FormatPlayerInfo(string accountName, string charName, string level)
{
    int padRight = 45;
    string account = String.Format("- {0} ({1})", accountName,
                                                charName)
                            .PadRight(padRight);
    return String.Format("{0} - Level: {1}", account, level);
}

Usage:

file.WriteLine(FormatPlayerInfo(
                 player.accountName, 
                 player.charName, 
                 player.level));
于 2013-03-05T16:16:17.097 回答