Is there an equivalent in D for C function "strstr" that return the first occurrence of a given string within another string?
Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
July 29, 2022 "strstr" D equivalent | ||||
---|---|---|---|---|
| ||||
July 29, 2022 Re: "strstr" D equivalent | ||||
---|---|---|---|---|
| ||||
Posted in reply to pascal111 | On 7/29/22 13:23, pascal111 via Digitalmars-d-learn wrote: > Is there an equivalent in D for C function "strstr" that return the first occurrence of a given string within another string? > > https://en.cppreference.com/w/c/string/byte/strstr You can use `find` from https://dlang.org/library/std/algorithm/searching/find.html for that. And since you asked about std lib functions in another thread, the algorithm docs page has lots of useful functions and examples for searching, comparing, iterating, etc: https://dlang.org/library/std/algorithm.html |
July 29, 2022 Re: "strstr" D equivalent | ||||
---|---|---|---|---|
| ||||
Posted in reply to pascal111 | On Friday, 29 July 2022 at 11:23:55 UTC, pascal111 wrote: >Is there an equivalent in D for C function "strstr" that return the first occurrence of a given string within another string? https://dlang.org/library/std/string/index_of.html I never want to break your fervour. But D has what you've been asking for a few days, or even better. Because we have a strong type like
Search and you will definitely find it. If you can't find it, D handles this kind of thing anyway. SDB@79 |
July 29, 2022 Re: "strstr" D equivalent | ||||
---|---|---|---|---|
| ||||
Posted in reply to Salih Dincer | Short version:
SDB@79 |
July 29, 2022 Re: "strstr" D equivalent | ||||
---|---|---|---|---|
| ||||
Posted in reply to Salih Dincer | On Friday, 29 July 2022 at 13:44:47 UTC, Salih Dincer wrote: >Short version:
SDB@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?! |
July 29, 2022 Re: "strstr" D equivalent | ||||
---|---|---|---|---|
| ||||
Posted in reply to pascal111 | On Friday, 29 July 2022 at 14:14:54 UTC, pascal111 wrote: >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?! If you scroll down further, you will see that there is a second set of overloads for The reason there are two sets of overloads, with separate documentation, is that one set searches for a single character in a string, while the other set searches for a substring in a string. |
July 29, 2022 Re: "strstr" D equivalent | ||||
---|---|---|---|---|
| ||||
Posted in reply to pascal111 | 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. It can also be written as:
SDB@79 |
July 29, 2022 Re: "strstr" D equivalent | ||||
---|---|---|---|---|
| ||||
Posted in reply to Salih Dincer | 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. It can also be written as:
SDB@79 I made this version: auto d_strstr (const string ch, const string substr)
} https://github.com/pascal111-fra/D/blob/main/dcollect.d ////////////testing program module main; import std.stdio;
|