I got a method which receives a message and a Priority enum and returns a formatted string.
private string FormatMessage(string message, Priority priority)
{
return string.Format("*{0,-6}* - {1}", priority, message);
}
Priority has three possible values: High, Medium and Low.
I'm using string.Format's alignment option so that the output would look nice. What I'd like the output to look like is this:
*Low* - First message
*Medium* - Second message
*Low* - Third message
However, what I'm getting is this:
*Low * - First message
*Medium* - Second message
*Low * - Third message
I understand why this is happening, but what I'd like to know is whether there's an easy (and correct) way to get the wanted output by using string.Format and without introducing any new variables.
string.Format("{0,-8} - {1}", "*" + priority + "*", message);
Or, if you're feeling fancy:
string.Format("{0,-8} - {1}", string.Format("*{0}*", priority), message);
string.Format("{0,-8} - {1}", string.Join(priority, new [] {"*", "*"}), message);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With