c# - 格式化没有 string.Format 的字符串?

标签 c# string.format

我添加了一个类型为 int 的变量,我用它来调用如下内容:

x.ToString("0000");

我将 x 更改为键入 string,现在上面的内容无效。是现在格式化 x 的唯一方法:

string.Format("{0:0000}",x);

或者有捷径吗?

我正在使用 StringBuilder 构建字符串,那么同样适用于 AppendFormat 吗?

最佳答案

您不能像处理数值那样格式化字符串,因此如果要对字符串应用数字格式,您必须先将其解析为数字:

Int32.Parse(x).ToString("0000")

关于c# - 格式化没有 string.Format 的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13087942/

相关文章:

c# - 在 PowerShell v2 中编译新类型 - Cookie Aware WebClient

c# - DRY 字符串格式

java - 在android中使用默认语言格式化字符串

c# - 使用 Noda Time,如何使用 LocalDate 和 LocalTime 创建 LocalDateTime

c# - 是什么让表单成为根引用?

c# - 如何筛选使用 Visual Studio 创建的 C# Winform datagridview

c# - 如何在 Convert.TryFromBase64String() 中使用 Span?

c# - TextBoxFor 上货币的 String.Format

c# - CultureInfo.CurrentCulture 在 String.Format() 中真的有必要吗?

c# - 解释令人困惑的条件字符串格式