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
7
  • You don't need String.Format if you are using $"". Commented Apr 27, 2017 at 23:49
  • The "return" returns it to my main in which it is written out. Is that what needs to be edited? Commented Apr 27, 2017 at 23:52
  • No, you can remove String.Format( and the closing ) --> return $"{DayValues}|{MonthValues}|{YearValues}|{TimeValues}|{TimestampValues}|{RegionValues}|{DepthValues}|{LatitudeValues}|{LongitudeValues}|{MagnitudeValues}|{IrisValues}"; Commented Apr 27, 2017 at 23:53
  • and then do what? Commented Apr 27, 2017 at 23:54
  • 1
    If you want to center something, you need to do the calculation for the padding yourself. Commented Apr 28, 2017 at 0:25

2 Answers 2

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}";
}
Sign up to request clarification or add additional context in comments.

2 Comments

@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.
@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 . msdn.microsoft.com/en-us/library/…
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.

8 Comments

Thanks Man. Much appreciated
This is definitely the long way to do it.
And the short way is...?
Really nice, didn't knew that interpolated strings would allow to pad data.
@Gusman it has nothing to do with interpolated string ..... you can add any code in {}
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.