Reputation: 27
I have a string which I would like to format in the style of a table. Currently it outputs all the data but not in proper columns / rows. I would like it so that they line up and are not overlapping. Any help on what to do? Thanks
Code that returns values:
public override string ToString()
{
return String.Format($"{DayValues}|{MonthValues}|{YearValues}|{TimeValues}|{TimestampValues}|{RegionValues}|{DepthValues}|{LatitudeValues}|{LongitudeValues}|{MagnitudeValues}|{IrisValues}");
}
Example of output:
10|January |2014|07:20:46|1389338446|TURKEY|10.300|39.460|27.950|4.000|4371306
31|December |2013|15:06:14|1388502374|CRETE|26.500|34.390|25.310|4.200|4370886
29|December |2013|06:54:59|1388300099|NORTHWESTERN BALKAN REGION|10.000|43.100|17.190|4.700|4370801
28|December |2013|18:55:03|1388256903|CYPRUS REGION|46.900|35.670|31.350|4.500|4370887
Upvotes: 0
Views: 73
Reputation: 415715
public override string ToString()
{
return $"{DayValues,2}|{MonthValues,-10}|{YearValues}|{TimeValues,8}|{TimestampValues,10}|{RegionValues,-40}|{DepthValues,6:#0.000}|{LatitudeValues,6:#0.000}|{LongitudeValues,6:#0.000}|{MagnitudeValues,6:#0.000}|{IrisValues,7}";
}
Upvotes: 2
Reputation: 15151
For what you want you need to pad the data to a maximum size, something like this:
public override string ToString()
{
return $"{DayValues.ToString().PadRight(2, ' ')}|{MonthValues.ToString().PadRight(10, ' ')}|{YearValues.ToString().PadRight(4, ' ')}|{TimeValues.ToString().PadRight(8, ' ')}|{TimestampValues.ToString().PadRight(10, ' ')}|{RegionValues.ToString().PadRight(40, ' ')}|{DepthValues.ToString().PadRight(6, ' ')}|{LatitudeValues.ToString().PadRight(6, ' ')}|{LongitudeValues.ToString().PadRight(6, ' ')}|{MagnitudeValues.ToString().PadRight(6, ' ')}|{IrisValues.ToString().PadRight(6, ' ')}";
}
I've left-aligned all the data but you can change the alignment using PadLeft
instead of PadRight
. Also, you need to adjust the column size (the pad length) to the maximum expected size for each value.
Upvotes: 1