【问题标题】:integer to string conversion in DD中的整数到字符串转换
【发布时间】:2012-05-24 17:29:07
【问题描述】:

如何在 D 中将整数转换为字符串? 类似的东西

int i = 15
string message = "Value of 'i' is " ~ toString(i); // cast(string) i - also does not work 

Google 为我提供了如何使用 tango 的答案,但我想要 phobos 版本。

【问题讨论】:

    标签: casting d phobos


    【解决方案1】:
    import std.conv;
    
    int i = 15;
    string message = "Value of 'i' is " ~ to!string(i);
    

    format:

    import std.string;
    string message = format("Value of 'i' is %s.", i);
    

    【讨论】:

      【解决方案2】:

      使用来自 std.conv 的to

      int i = 15
      string message = "Value of 'i' is " ~ to!string(i);
      

      【讨论】:

        【解决方案3】:
        import std.conv;
        auto i = 15;
        auto message = text("Value of 'i' is ", i);
        

        还有 wtext 和 dtext 变体,巫婆返回 wstring 和 dstring。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-01
          • 2017-10-16
          • 2015-08-10
          • 2014-02-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多