string find in C++
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 indices of all occurrence of one string in other
- 5 Different methods to find length of a string in C++
- Find and print duplicate words in std::vector<string> using STL functions
- Check if a string can be formed from another string using given constraints
- std::string::append vs std::string::push_back() vs Operator += in C++
- std::string::remove_copy(), std::string::remove_copy_if() in C++
- std::string::replace_copy(), std::string::replace_copy_if in C++
- std::string::replace , std::string::replace_if in C++
- string at() in C++
- Prime String
- std::string::insert() in C++
- How to get the last n characters of a PHP string?
- std::string::erase in C++
- Raw string literal in C++
- std::string::clear in C++
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.



