How to extend the string class to return this inside the square bracket?

Paul Backus snarwin at gmail.com
Fri Aug 13 23:33:05 UTC 2021


On Friday, 13 August 2021 at 23:23:55 UTC, Marcone wrote:
>
> writeln("Hello World!"[x.indexOf("e")..x.indexOf("r")]);
>
> indexOf()is just a simple example, not the goal. I want handle 
> literal inside [] like it bellow, but in literal:
>
> string x = "Hello World!";
> writeln(x[x.indexOf("e")..x.indexOf("r")]);

You can use the `pipe` function to bind an arbitrary expression 
to a variable:

```d
import std.functional: pipe;
import std.algorithm: countUntil;
import std.stdio: writeln;

"Hello world!"
     .pipe!(s => s[s.countUntil('e') .. s.countUntil('r')])
     .writeln;
```


More information about the Digitalmars-d-learn mailing list