Вопрос

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.

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top