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@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.

Reply via email to