"strstr" D equivalent

pascal111 judas.the.messiah.111 at gmail.com
Fri Jul 29 14:14:54 UTC 2022


On Friday, 29 July 2022 at 13:44:47 UTC, Salih Dincer wrote:
> **Short version:**
>
> ```d
> import std.string;
> import std.stdio;
>
> void find (string str, string substr) {
>     if(auto pos = str.indexOf(substr)) {
>         writefln("found the string '%s' in '%s' at position: 
> %s", substr, str, pos);
>     } else {
>         writefln("the string '%s' was not found in '%s'", 
> substr, str);
>     }
> }
>
> void main() {
>     string str = "one two three";
>     str.find("two");
>     str.find("");
>     str.find("nine");
>     str.find("n");
> }
> ```
> SDB at 79

Ok! I have a problem now in understanding these new syntax. You 
said "str.indexOf(substr)", so I can say that the first parameter 
of "indexOf" is "str" itself, and second parameter is "substr" 
and rather than writing it in the form "indexOf(str, substr)" you 
written it like "str.indexOf(substr)", am I right? and if I'm 
right, with returning back to the definitions of "indexOf" @ 
https://dlang.org/phobos/std_string.html#.indexOf we won't find 
that there is a definition for it with just two parameters, so 
from where you got this new definition of this function?!


More information about the Digitalmars-d-learn mailing list