An introduction to C++ String Discover Perform
- The string discover() operate in C++ is used to discover a substring within the given string.
- The discover() operate belongs to the C++ string class.
- We should embrace <string> within the header part (see the applications under)
- For instance, we’ve the string “That is C++ tutorial”.
- We need to discover if the above string accommodates the phrase “tutorial” or not.
- Within the discover() operate of C++, we will specify that string and supply the phrase “tutorial”.
- The discover technique returns the place of the phrase if discovered.
- If the place of the given phrase/character is discovered – which means the phrase exists – in any other case it’s not.
- If the searched string isn’t discovered the discover technique return string::npos. npos is the static member fixed worth with the best worth for a component of size_t.
Syntax of utilizing C++ discover() operate
Following is the syntax for utilizing the discover() operate:
str.discover(seq, place, rely)
Following are the parameters description of discover operate:
- Str: is the string object. The string you need to search in.
- Seq: It’s the sequence of characters that you just need to search within the str.
- Pos: place or index within the str to begin the search. The default is 0. It’s optionally available.
- Rely: Variety of characters that should match within the str. The default is the size of the seq. That is additionally an optionally available parameter.
Now allow us to take a look at just a few examples of utilizing the string discover operate with a number of parameters.
An instance of C++ discover with a single parameter
On this instance, we solely used the required parameter within the discover() operate. Now we have created a string (str1) and used it with the discover technique.
One other string variable is said (str2) which is used because the sequence to look within the discover technique. See the code and output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#embrace <iostream>
#embrace <string>
utilizing namespace std;
int primary(){
//Creating string object to look in
string str1 = “That is C++ tutorial”;
//substring or seq to look in str1
string str2 = “tutorial”;
//Utilizing discover operate
cout <<“The tutorial begins at: “ << str1.discover(str2);
} |
Output:
What if a given sequence happens twice or extra?
On this instance, we looked for a personality ‘s’ within the given string. It happens twice within the sentence. See what it outputs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#embrace <iostream>
#embrace <string>
utilizing namespace std;
int primary(){
//Creating string object to look in
string str1 = “Hold on, I’m coming!”;
//substring or seq to look in str1
string str2 = “o”;
//Utilizing discover operate
cout <<“The o begins at: “ << str1.discover(str2);
} |
Output:
An instance with sequence and place parameter
In this system under, we used two parameters within the discover operate. The primary is the sequence (similar to the above instance i.e. str2) and the second is the place parameter. First, take a look on the code and output under:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#embrace <iostream>
#embrace <string>
utilizing namespace std;
int primary(){
//Creating string object to look in
string str1 = “Have a pleasant day!”;
//substring or seq to look in str1
string str2 = “good”;
//Utilizing discover operate with place parameter
cout <<“The good begins at: “ << str1.discover(str2, 3);
} |
First output as we set place 3:
Once we set place 8:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#embrace <iostream>
#embrace <string>
utilizing namespace std;
int primary(){
//Creating string object to look in
string str1 = “Have a pleasant day!”;
//substring or seq to look in str1
string str2 = “good”;
//Utilizing discover operate with place parameter
cout <<“The good begins at: “ << str1.discover(str2, 8);
} |
- As sequence/phrase “good” begins at place 7. Within the first program, we see the output as 7.
- Nevertheless, within the second program, we set the looking place to begin at 8.
- Because the discover operate was unable to search out the phrase “good” from place 8, so it returned string::npos. Which is described within the first part.
What if the sequence happens twice?
We are going to seek for the letter ‘o’ on this instance. It happens twice within the given sentence. Nevertheless, the place is given so the primary incidence must be bypassed.
Allow us to see the output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#embrace <iostream>
#embrace <string>
utilizing namespace std;
int primary(){
//Creating string object to look in
string str1 = “Hold on, I’m coming!”;
//substring or seq to look in str1
string str2 = “o”;
//Utilizing discover operate
cout <<“The o begins at: “ << str1.discover(str2, 6);
} |
Output:
You noticed, although the primary incidence is at place 5, it didn’t return. The reason being we set place 6. So it returned the following incidence of ‘s’ which is 15.
Utilizing all three parameters instance
Within the following program, we additionally used a 3rd optionally available parameter i.e. rely. See the code and output under:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#embrace <iostream>
#embrace <string>
utilizing namespace std;
int primary(){
//Creating string object to look in
string str1 = “Have a pleasant day!”;
//Utilizing discover operate with place and rely parameter
cout <<“The good begins at: “ << str1.discover(“nicee”, 7, 3);
} |
Output:
`