"strstr" D equivalent
pascal111
judas.the.messiah.111 at gmail.com
Fri Jul 29 20:00:58 UTC 2022
On Friday, 29 July 2022 at 15:39:16 UTC, Salih Dincer wrote:
> On Friday, 29 July 2022 at 14:14:54 UTC, pascal111 wrote:
>> 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?!
>
> This thread is about
> [UFCS](https://tour.dlang.org/tour/en/gems/uniform-function-call-syntax-ufcs). It can also be written as:
>
> ```d
> import std.string,
> std.stdio;
>
> void findStr(string str) { }
>
> void main()
> {
> string str = "Hello, World!";
> str.indexOf("Wo").writeln; //7
> auto pos = "Hello, World!".indexOf("Wo");
>
> auto slice = str[pos..$-1];
> slice.writeln(": ", slice.length); // World: 5
> typeid(slice).writeln; // immutable(char)[]
>
> assert(str.indexOf("Wo") == pos);
>
> void find_str(string str) { }
>
> find_str(str);
> str.findStr();
>
> /* No compile:
>
> slice.typeid().writeln;
>
> slice.indedOf("Wo").assert();
>
> str.find_str();
> */
> }
> ```
>
> SDB at 79
I made this version:
auto d_strstr (const string ch, const string substr)
{
return ch.indexOf(substr);
}
https://github.com/pascal111-fra/D/blob/main/dcollect.d
////////////testing program
module main;
import std.stdio;
import std.string;
import std.conv;
import dcollect;
import std.math;
int main(string[] args)
{
int x;
char[] ch;
string ch1="Hello World!";
writeln(ch1.d_strstr("ll"));
return 0;
}
More information about the Digitalmars-d-learn
mailing list