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

Ali Çehreli acehreli at yahoo.com
Fri Aug 13 23:35:54 UTC 2021


On 8/13/21 4:23 PM, Marcone wrote:

 > string x = "Hello World!";
 > writeln(x[x.indexOf("e")..x.indexOf("r")]);

I don't see the usefulness and there are the following problems with it:

- Not an algorithmic complexity issue but it sounds to me like a 
pessimization to go through the elements in linear fashion, obtain 
indexes and then iterate between the indexes again.

- This approach requires random access, which only a subset of 
collections provide.

- The semantics of the second index is not clear. What is the intent 
when we say "from 'f' to 'n'" in the string "confusing"? Is it an error 
because the indexes 3..2 would be illegal? Or did we mean second 'n' in 
the string?

On the other hand, the programmer can build any semantic with the 
existing range algorithms. And that would work even with InputRanges.

You didn't ask but sorry, this feature is not for me. :)

Ali



More information about the Digitalmars-d-learn mailing list