Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在此示例中,对齐组件是:-20
-20
$"{value, -20}"
有没有办法制作这样的插值字符串:
$"{value, alignment}"
alignment变量在哪里?
alignment
不幸的是,alignment不能是变量。alignment必须是一个常量表达式。这是文档中的相关部分
对齐: 常量表达式,其值定义了插值表达式结果的字符串表示中的最小字符数。如果为正,则字符串表示为右对齐;如果为负,则为左对齐。有关详细信息,请参阅对齐组件。
如前所述,对齐必须保持不变,但可以尝试以类似的方式使用 Padleft(不确定这是否适合您)。
那或者必须对 1 个字符串进行各种字符串插值。
string test; if (value.length > 100) test = "${value: -20}"; else test = "${value : 20}";
希望这可以帮助。