Fast removal of character

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Oct 11 22:45:14 UTC 2017


On Wednesday, October 11, 2017 22:22:43 Johan Engelen via Digitalmars-d-
learn wrote:
> std.string.removechars is now deprecated.
> https://dlang.org/changelog/2.075.0.html#pattern-deprecate
>
> What is now the most efficient way to remove characters from a
> string, if only one type of character needs to be removed?
>
> ```
> // old
> auto old(string s) {
>      return s.removechars(",").to!int;
> }
>
> // new?
> auto newnew(string s) {
>      return s.filter!(a => a != ',').to!int;
> }
> ```

Well, in general, I'd guess that the fastest way to remove all instances of
a character from a string would be std.array.replace with the replacement
being the empty string, but if you're feeding it to std.conv.to rather than
really using the resultant string, then filter probably is faster, because
it won't allocate. Really though, you'd have to test for your use case and
see how fast a given solution is.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list