Question

If I have a C string, which is a pointer to a null terminated character array, how do I convert it to a regular D string?

The reason I'd like to know this is because I'm currently using an external C library which on error returns C strings.

Was it helpful?

Solution

Use the std.conv.to function:

char* c_str = c_function();
string s = to!string(c_str);

You can also slice and/or duplicate the c string:

char[] arr = c_str[0 .. strlen(c_str)];
string s = arr.idup;

and so on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top