to delete the '\0' characters

user1234 user1234 at 12.de
Thu Sep 22 13:29:43 UTC 2022


On Thursday, 22 September 2022 at 10:53:32 UTC, Salih Dincer 
wrote:
> Is there a more accurate way to delete the '\0' characters at 
> the end of the string? I tried functions in this module: 
> https://dlang.org/phobos/std_string.html
>
> ```d
> auto foo(string s)
> {
>   string r;
>   foreach(c; s)
>   {
>     if(c > 0)
>     {
>       r ~= c;
>     }
>   }
>   return r;
> }
> ```
>
> SDB at 79

Two remarks:

1. The zero implicitly added to literals is not part of the 
string. for example s[$-1] will not give 0 unless you added it 
explictly to a literal

2. you code remove all the 0, not the one at the end. As it still 
ensure what you want to achieve, maybe try 
[`stripRight()`](https://dlang.org/phobos/std_string.html#.stripRight). The second overload allows to specify the characters to remove.


More information about the Digitalmars-d-learn mailing list