【问题标题】:Equivalent of Format of VB in C#C#中VB格式的等价物
【发布时间】:2011-03-18 19:41:37
【问题描述】:

Format(iCryptedByte, "000") (VB.NET) 在 C# 中的等效代码是什么?

【问题讨论】:

    标签: c# .net vb.net vb.net-to-c#


    【解决方案1】:

    鉴于此 VB 代码:

    Strings.Format(iCryptedByte, format)
    

    用这个 C# 代码替换:

    var csformat = "{0:" + format + "}";
    String.Format(csformat, iCryptedByte);
    

    【讨论】:

      【解决方案2】:
      Microsoft.VisualBasic.Strings.Format(iCryptedByte, "000");
      

      您需要添加对 Microsoft.VisualBasic 程序集的引用。

      【讨论】:

        【解决方案3】:

        另一个非常有用的 C# 字符串格式化网站:http://blog.stevex.net/string-formatting-in-csharp/

        除了{0:D3},您还可以使用零占位符,例如{0:000} 将用零填充到最小长度为 3。

        【讨论】:

          【解决方案4】:

          试试:

          iCryptedByte.ToString("D3");
          

          【讨论】:

          • 取决于iCryptedByte 类型。如果是int - 是的,ToString(string) 存在
          【解决方案5】:

          【讨论】:

            【解决方案6】:
            String.Format(format, iCryptedByte); // where format like {0:D2}
            

            参见 MSDN 123

            【讨论】:

              猜你喜欢
              • 2017-12-20
              • 2011-03-09
              • 2015-11-02
              • 1970-01-01
              • 2019-04-01
              • 1970-01-01
              • 1970-01-01
              • 2022-06-11
              • 1970-01-01
              相关资源
              最近更新 更多