加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用String Interpolation将字符串格式化为列

发布时间:2020-12-15 23:39:30 所属栏目:百科 来源:网络整理
导读:我需要打印双打,以便为字符串表示值分配确定数量的符号(如8).下一个单词应该从每个字符串中字符串开头的相同索引处开始.我现在有: value: 0 testvalue: 0.3333333333333 testvalue: 0.5 test 我需要: value: 0 testvalue: 0.33333333 testvalue: 0.5 test
我需要打印双打,以便为字符串表示值分配确定数量的符号(如8).下一个单词应该从每个字符串中字符串开头的相同索引处开始.我现在有:

value: 0 test
value: 0.3333333333333 test
value: 0.5 test

我需要:

value: 0           test
value: 0.33333333  test
value: 0.5         test

测试代码:

double[] ar = new double[] { 0,(double)1 / 3,(double)1 / 2 };
string s = "test";

foreach (var d in ar)
{
    Console.WriteLine($"value: {d} {s}");
}

在{d :?之后我应该添加什么?

解决方法

您可以使用 Alignment Component来实现此目的.像这样:

Console.WriteLine($"value: {d,-17} {s}");

The optional alignment component is a signed integer indicating the preferred formatted field width. If the value of alignment is less than the length of the formatted string,alignment is ignored and the length of the formatted string is used as the field width. The formatted data in the field is right-aligned if alignment is positive and left-aligned if alignment is negative. If padding is necessary,white space is used. The comma is required if alignment is specified.

所以这就是我们使用负对齐的原因,因为您希望第一列是左对齐的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读