具有可变空间对齐方式的C#字符串格式 [英] C# string formatting with variable space alignment

查看:79
本文介绍了具有可变空间对齐方式的C#字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做类似的事情

String.Format("Completed {0:9} of ",0) + xlsx.totalCount.ToString();

除了要对9进行硬编码外,我希望对齐方式是xlsx.totalCount.有什么想法吗?

except instead of hardcoding a 9 I want the alignment to be whatever xlsx.totalCount is. Any thoughts?

推荐答案

该字符串不必是编译时间常数,您可以在运行时构建字符串(使用StringBuilder,operator +或嵌套的String.Format.).例如,这将产生所需的字符串,其中xlsx.totalCount替换为"9":

The string doesn't have to be a compile time constant, you can build the string during runtime (using a StringBuilder, operator+ or even a nested String.Format). This, for instance will produce the needed string with xlsx.totalCount replacing the "9":

String.Format("Completed {0:" + xlsx.totalCount + "} of "...

这篇关于具有可变空间对齐方式的C#字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆