Reputation: 41
I'm trying to format a string with the Format() method unfortunately i get this result with a foreach loop:
Code:
output += String.Format("{0,17} {1,48}\n\n", "Name", "Description " );
Console.WriteLine(output);
string outputloop;
foreach (IMetricContract contract in metrics)
{
outputloop = String.Format("{0,17} {1,48}\n", contract.Name, contract.Description);
Console.WriteLine(outputloop);
}
You can see that the elements are in a different position, but i have defined the same values. Anyone knows an solution?
Edit:
outputloop = String.Format("{0,-17} {1,-48}\n", contract.Name,
I also tried this with negative values for the position but then i get this
The Result should be look like this (Edit with Imageeditor)
Upvotes: 0
Views: 424
Reputation: 41
So finally i found out how to solve this task, instead of the format function i used the padright function for strings in c#. This two sites gave me help:
http://www.dotnetperls.com/padright
https://msdn.microsoft.com/en-us/library/66f6d830(v=vs.110).aspx
Here is my code:
foreach (IMetricContract contract in metrics)
{
name = contract.Name;
desc = contract.Description;
Console.Write("".PadRight(13,'#')+name.PadRight(41,'i'));
Console.WriteLine(desc + "\n");
}
Upvotes: 0
Reputation: 61952
If I understand correctly, I think you want negative numbers, as in:
outputloop = String.Format("{0,-17} {1,-48}\n", contract.Name, contract.Description);
This makes it aligned to the left (instead of to the right).
Whether you use positive or negative numbers, when the actual number of characters exceeds the absolute value of 48
(in our example), the "extra" characters go outside the "field" to the right.
Upvotes: 3