Reputation: 138
Printing out a deck of cards I have a tostring in my class that reads
public override string ToString(){
return GetCard() + " of " + GetSuit();}//end ToString
but I want all of the results to be in even columns so I tried
public override string ToString(){
return "{0,5}of{1}",GetCard(),GetSuit();}//end ToString
and I don't think I can do that and I get an error. Is there another way to modify a tostring so I make even columns?
Upvotes: 0
Views: 94
Reputation: 138
solved using
public override string ToString(){
return $"{GetCard(),-2} of {GetSuit()}";}//end ToString```
Upvotes: 1
Reputation: 186813
You, probably, are looking for string interpolation:
public override string ToString() => $"{GetCard(),5} of {GetSuit}";
please, note the syntax: $
before the interpolated atring and values in braces "...{value}..."
Upvotes: 2