Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert array of ints to string?

I need convert array of ints to string. Following code doing it, but in result I am getting unwanted symbols [ ]

import std.stdio;
import std.conv;

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

output: [1, 3, 4, 6] How I can remove brackets without hack with replace?

like image 888
Dmitry Bubnenkov Avatar asked Mar 12 '23 15:03

Dmitry Bubnenkov


2 Answers

You can do it for example like this:

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

void main()
{
    int [] x = [1,3,4,6];
    writeln(x.map!(to!string).joiner(", "));
}
like image 147
Tamas Avatar answered Mar 15 '23 05:03

Tamas


You can use 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
}
like image 20
Kozzi11 Avatar answered Mar 15 '23 05:03

Kozzi11