String find is used to find the first occurrence of sub-string in the specified string being called upon. It returns the index of the first occurrence of the substring in the string from given starting position. The default value of starting position is 0.
Function Template:
- size_t find (const string& str, size_t pos = 0);
- size_t find (const char* s, size_t pos = 0);
Function parameters:
- str : The sub-string to be searched.
- s : The sub-string to be searched, given as C style string.
- pos : The initial position from where the string search is to begin.
Function Return:
- The function returns the index of the first occurrence of sub-string, if the sub-string is not found it returns string::npos(string::pos is static member with value as the highest possible for the size_t data structure).
// CPP program to demonstrate working of string // find to search a string #include <iostream> #include <string> using namespace std; int main() { string str = "geeksforgeeks a computer science"; string str1 = "geeks"; // Find first occurrence of "geeks" size_t found = str.find(str1); if (found != string::npos) cout << "First occurrence is " << found << endl; // Find next occurrence of "geeks". Note here we pass // "geeks" as C style string. char arr[] = "geeks"; found = str.find(arr, found+1); if (found != string::npos) cout << "Next occurrence is " << found << endl; return 0; } |
First occurrence is 0 Next occurrence is 8
We can also use it to find occurrence of a character:
In below syntax, note that c is a character.
- size_t find (const char c, size_t pos = 0);
// CPP program to demonstrate working of string // find to search a string #include <iostream> #include <string> using namespace std; int main() { string str = "geeksforgeeks a computer science"; char c = 'g'; // Find first occurrence of 'g' size_t found = str.find(c); if (found != string::npos) cout << "First occurrence is " << found << endl; // Find next occurrence of 'g' found = str.find(c, found+1); if (found != string::npos) cout << "Next occurrence is " << found << endl; return 0; } |
First occurrence is 0 Next occurrence is 8
We can also search for a partial string
In below syntax, note that n is number of characters to match.
- size_t find (const char *str, size_t pos, size_t n);
// CPP program to demonstrate working of string // find to search a string #include <iostream> #include <string> using namespace std; int main() { string str = "geeksforgeeks a computer science"; // Only search first 5 characters of "geeks.practice" size_t found = str.find("geeks.practice", 0, 5); if (found != string::npos) cout << found << endl; return 0; } |
0
Recommended Posts:
- Find length of longest subsequence of one string which is substring of another string
- Find the character in first string that is present at minimum index in second string
- Find the count of palindromic sub-string of a string in its sorted form
- Find the smallest window in a string containing all characters of another string
- Find if a string starts and ends with another given string
- Find a palindromic string B such that given String A is a subsequense of B
- Find sub-string with given power
- Find the direction from given string
- Find one extra character in a string
- Find n-th lexicographically permutation of a string | Set 2
- Find all palindromic sub-strings of a given string | Set 2
- Find if an array contains a string with one mismatch
- 5 Different methods to find length of a string in C++
- Find the first repeated word in a string
- Find indices of all occurrence of one string in other
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



