How to convert string C to string D?

If I have a C string, which is a pointer to an array of characters with a null terminating character, how can I convert it to a regular D string?

The reason I would like to know this is because I am currently using an external C library, which returns C strings on error.

+4
source share
1 answer

Use function std.conv.to:

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

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

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

etc.

+6
source

All Articles