Alternatives to “ ” for creating strings containing multiple whitespace characters

后端 未结 12 1422
粉色の甜心
粉色の甜心 2020-12-09 15:30

I\'m wondering if there\'s a more OO way of creating spaces in C#.

Literally Space Code!

I currently have tabs += new String(\" \"); and I can\'

12条回答
  •  孤城傲影
    2020-12-09 16:26

    The more "OO" way would be to find a simpler way of solving your larger business problem. For example, the fact that you have a variable named tabs suggests to me that you are trying to roll your own column alignment code. String.Format supports that directly, e.g.

    // Left-align name and status, right-align amount (formatted as currency).
    writer.WriteLine("Name                 Status         Amount");
    writer.WriteLine("-------------------- ---------- ----------");
    foreach(var item in items) {
        writer.WriteLine(string.Format("{0,-20} {1,-10} {2,10:C}", item.Name, item.Status, item.Amount));
    }
    

提交回复
热议问题