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

jfondren julian.fondren at gmail.com
Fri Aug 13 23:31:32 UTC 2021


On Friday, 13 August 2021 at 23:23:55 UTC, Marcone wrote:
> On Friday, 13 August 2021 at 23:08:07 UTC, jfondren wrote:
>> On Friday, 13 August 2021 at 22:09:59 UTC, Marcone wrote:
>>>
>>> Isn't there some unario operator template that I can use with 
>>> lambda to handle a string literal?
>>
>> So, something other than an exact "lit"[0..this.xx(..)] syntax 
>> is fine?
>>
>> What didn't you like about `"Hello 
>> World!".findSplit("o")[0].writeln;` then?
>>
>> What is a real example of something you want to do?
>
> 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")]);

```d
unittest {
     import std.functional : pipe;
     import std.string : indexOf;

     assert("Hello, world!".pipe!(x => x[x.indexOf("e") .. 
x.indexOf("r")])
             == "ello, wo");
}
```


More information about the Digitalmars-d-learn mailing list