如何将整数数组转换为字符串?

How to convert array of ints to string?

我需要将 int 的数组转换为字符串。按照代码执行此操作,但结果我得到了不需要的符号 [ ]

import std.stdio;
import std.conv;

void main()
{
    int [] x = [1,3,4,6];
    string s = to!string(x);
    writeln(s);
}

输出:[1, 3, 4, 6] 如何使用 replace?

在不破解的情况下删除括号

你可以这样做,例如:

import std.stdio;
import std.conv;
import std.algorithm;

void main()
{
    int [] x = [1,3,4,6];
    writeln(x.map!(to!string).joiner(", "));
}

您可以使用std.format

import std.format;
import std.stdio;

void main()
{
    auto res = format("%(%s, %)", [1,2,3,4,5]);
    writeln(res); // output: 1, 2, 3, 4, 5
}