0

I have got string like: string mystr = "1,2,3,4,5"; I need convert it to array of int's like: int [] myints. How can I do it?

Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145
  • 1
    conversion see [d programming, parse or convert string to double](https://stackoverflow.com/questions/12605641/d-programming-parse-or-convert-string-to-double) for inspiration – Patrick Artner Dec 10 '17 at 15:45
  • no, arrays can be converted differently which makes the answer here more unique – WebFreak001 Dec 10 '17 at 15:45

1 Answers1

6

If you format your string like "[1,2,3]" ('[' ~ mystr ~ ']') you can just import std.conv and call mystr2.to!(int[]). (whitespaces don't matter here, as long as it starts with [ and ends with ])

Otherwise if you have an array of int strings (["1", "2", "3"]), like you would get from a split call you can use myarr.to!(int[]) too and it will convert each string to an int. (make sure you don't have whitespaces here, otherwise you would call .map!(a => a.strip.to!int).array)

Basically to can do everything on arrays which it can do on primitives too.

WebFreak001
  • 2,415
  • 1
  • 15
  • 24