what exactly is string length?

rikki cattermole rikki at cattermole.co.nz
Fri Apr 2 05:20:50 UTC 2021


On 02/04/2021 6:10 PM, Computermatronic wrote:
> On Friday, 2 April 2021 at 05:02:52 UTC, mw wrote:
>> Ahh, I got what I see (from writeln) is not what get string here ;-)
>>
>> And I just tried:
>>
>> string t = text("head-", strip(s), "-tail");
>>
>> It's the same behavior.
>>
>> So how can I trim the leading & trailing `\0` from the static char array?
> 
> strip only removes whitespace, not null characters. You'd have to do 
> something like
> ```d string t = cast(string)text("head-", s, "-tail").filter!`a != 
> '\0'`().array;```
> I would assume there would be a better way, but I haven't been able to 
> find a dedicated function for stripping null chars in std.

If you know it has null terminators you can use fromStringz.
But this is a case of you should store the length.


More information about the Digitalmars-d-learn mailing list