Using char* and C code

simendsjo simendsjo at gmail.com
Wed Mar 6 23:35:22 PST 2013


On Thursday, 7 March 2013 at 07:20:16 UTC, Jeremy DeHaan wrote:
>
>> That's what toStringz is for, and it'll avoid appending the 
>> '\0' if it can
>> (e.g. if the code unit one past the end of the string is '\0' 
>> as it is with
>> string literals).
>>
>
>
> I actually have a different question related to this now that I 
> think about it. Is there a similar function to go from a '\0' 
> terminated char* to a D string? Lately I have been using 
> std.conv.text, but I have also made a function that just parses 
> the pointer and copies its data into a string. I'm actually 
> kind of surprised that there isn't anything built into the 
> string class like this.

You can use "blah\0".to!string(). You can easily use slices too: 
"blah\0"[0..$-1].
Remember that D doesn't have a string class. string is defined as 
this:
   alias immutable(char)[] string;
So it's just an array (but some compiler knowledge).


More information about the Digitalmars-d-learn mailing list