Pregunta

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.

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top