www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - "strstr" D equivalent

reply pascal111 <judas.the.messiah.111 gmail.com> writes:
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
Jul 29 2022
next sibling parent rassoc <rassoc posteo.de> writes:
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
Jul 29 2022
prev sibling parent reply Salih Dincer <salihdb hotmail.com> writes:
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://en.cppreference.com/w/c/string/byte/strstr
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 `immutable(char[])`. In summary, it only took me 1 minute to compile the same code: ```d // D 2.0.83 import std.string; import std.stdio; void find_str(string str, string substr) { import std.typecons : No; auto pos = indexOf(str, substr, No.caseSensitive); if(pos) { writef("found the string '%s' in '%s' at position: %s\n", substr, str, pos); } else { writef("the string '%s' was not found in '%s'\n", substr, str); } } int main() { string str = "one two three"; find_str(str, "two"); find_str(str, ""); find_str(str, "nine"); find_str(str, "n"); return 0; } ``` Search and you will definitely find it. If you can't find it, D handles this kind of thing anyway. SDB 79
Jul 29 2022
parent reply Salih Dincer <salihdb hotmail.com> writes:
**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 79
Jul 29 2022
parent reply pascal111 <judas.the.messiah.111 gmail.com> writes:
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 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?!
Jul 29 2022
next sibling parent Paul Backus <snarwin gmail.com> writes:
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 `indexOf`: https://dlang.org/phobos/std_string.html#.indexOf.2 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.
Jul 29 2022
prev sibling parent reply Salih Dincer <salihdb hotmail.com> writes:
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 79
Jul 29 2022
parent pascal111 <judas.the.messiah.111 gmail.com> writes:
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 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; }
Jul 29 2022