Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String splitting in the D language

Tags:

string

split

d

I am learning D and trying to split strings:

   import std.stdio;
   import std.string;

    auto file = File(path, "r"); 
    foreach (line; file.byLine) {
           string[] parts = split(line);

This fails to compile with:

Error: cannot implicitly convert expression (split(line)) of type char[][] to string[]

This works:

    auto file = File(path, "r"); 
    foreach (line; file.byLine) {
           char[][] parts = split(line);

But why do I have to use a char[][]? As far as I understand the documentation, it says that split returns a string[], which I would prefer.

like image 362
clstaudt Avatar asked Nov 18 '25 18:11

clstaudt


1 Answers

Use split(line.idup);

split is a template function, the return type depends on its argument. file.byLine.front returns a char[] which is also reused for performance reasons. So if you need the parts after the current loop iteration you have to do a dup or idup, whatever you need.

like image 193
robert Avatar answered Nov 22 '25 02:11

robert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!