0

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
Duncher
  • 27
  • 9
  • You don't need String.Format if you are using $"". –  Apr 27 '17 at 23:49
  • The "return" returns it to my main in which it is written out. Is that what needs to be edited? – Duncher Apr 27 '17 at 23:52
  • No, you can remove String.Format( and the closing ) --> return $"{DayValues}|{MonthValues}|{YearValues}|{TimeValues}|{TimestampValues}|{RegionValues}|{DepthValues}|{LatitudeValues}|{LongitudeValues}|{MagnitudeValues}|{IrisValues}"; –  Apr 27 '17 at 23:53
  • and then do what? – Duncher Apr 27 '17 at 23:54
  • It was just a sidenote. To answer your question, you can use PadRight to fill the column (with spaces) to the desired length. For RegionValues you'll need to take the max length, or choose a length to truncate the column. –  Apr 27 '17 at 23:56
  • Can I center the values? – Duncher Apr 27 '17 at 23:59
  • 1
    If you want to center something, you need to do the calculation for the padding yourself. – Joel Coehoorn Apr 28 '17 at 00:25

2 Answers2

2
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}";
}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • @saraansh if you can see this, I suggest undeleting your answer. Tabs are a great solution when _combined with_ formatted padding. The use of tabs helps things align better if you copy the text and then paste it somewhere like Excel. You use a format string like in this answer so everything is at least within a tab stop of your desired width, and put a single tab for the remaining width as the delimiter. – Joel Coehoorn Apr 28 '17 at 00:38
  • @JoelCoehoorn The key to understanding why this works is interpolation is a syntactic sugar on top of String.Format and it makes sense since formatting works that way . https://msdn.microsoft.com/en-us/library/txafckwd(v=vs.110).aspx?ranMID=24542&ranEAID=TnL5HPStwNw&ranSiteID=TnL5HPStwNw-lAIORGo7ATDUKOSCbLvlOg&tduid=(f0435b35deaf6e8f1d60a080615fd854)(256380)(2459594)(TnL5HPStwNw-lAIORGo7ATDUKOSCbLvlOg)() – loneshark99 Apr 28 '17 at 02:42
1

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.

Gusman
  • 14,905
  • 2
  • 34
  • 50