C# 字符串格式化与可变空间对齐

And*_*rew 5 c# string-formatting

我想做类似的事情

String.Format("Completed {0:9} of ",0) + xlsx.totalCount.ToString();
Run Code Online (Sandbox Code Playgroud)

除了不是硬编码 9 我希望对齐是 xlsx.totalCount 是什么。有什么想法吗?

Neo*_*ard 3

该字符串不必是编译时常量,您可以在运行时构建字符串(使用 StringBuilder、operator+ 甚至嵌套的 String.Format)。例如,这将生成所需的字符串,并用 xlsx.totalCount 替换“9”:

String.Format("Completed {0:" + xlsx.totalCount + "} of "...
Run Code Online (Sandbox Code Playgroud)