字符串插值中的变量对齐组件
Variable alignment component in string interpolation
本例中的alignment component就是-20
:
$"{value, -20}"
有没有办法制作这样的内插字符串:
$"{value, alignment}"
其中 alignment
是一个变量?
很遗憾,alignment
不能是变量。 alignment
必须是常量表达式。这是 docs
中的相关部分
alignment: The constant expression whose value defines the minimum number of characters in the string representation of the result of the interpolated expression. If positive, the string representation is right-aligned; if negative, it's left-aligned. For more information, see Alignment Component.
如前所述,对齐必须保持不变,但可以尝试以类似的方式使用 Padleft(不确定这是否适合您)。
或者必须对 1 个字符串进行各种字符串插值。
string test;
if (value.length > 100)
test = "${value: -20}";
else test = "${value : 20}";
希望对您有所帮助。
本例中的alignment component就是-20
:
$"{value, -20}"
有没有办法制作这样的内插字符串:
$"{value, alignment}"
其中 alignment
是一个变量?
很遗憾,alignment
不能是变量。 alignment
必须是常量表达式。这是 docs
alignment: The constant expression whose value defines the minimum number of characters in the string representation of the result of the interpolated expression. If positive, the string representation is right-aligned; if negative, it's left-aligned. For more information, see Alignment Component.
如前所述,对齐必须保持不变,但可以尝试以类似的方式使用 Padleft(不确定这是否适合您)。
或者必须对 1 个字符串进行各种字符串插值。
string test;
if (value.length > 100)
test = "${value: -20}";
else test = "${value : 20}";
希望对您有所帮助。