String find function in C++
String find is a function used to locate the initial instance of a substring inside the provided string. It returns the index of the substring's initial position in the string from the supplied beginning place. The initial starting point is set at 0 by default.
Syntax
This function is a member of the C++ string class (std::string). As a result, the header file
This must be called on a string object with another string as a parameter. The find() method will then determine whether the given string exists in our string. It will return the length of the substring regardless of the '0' termination character. However, if the string is not found in our initial string, it will return 0.
As a result, the function is outlined as follows:
find size_t(const std::string& my_string, size_t pos = 0);
Requirements
- str: a string to search for
- position to begin the search
- count—the size of the substring to search for
- s: reference to a character string to seek for; ch: character to search for
- t: object to check for (converting to std::basic_string_view)
Return Value:
- The function outputs the position of the sub-string's initial occurrence.
- If the sub-string is not present, it returns string::npos(string::pos is a static member with its value as the highest possible for the size_t data type).
Example 1:
#include#include int main() { Generate our string std::string my_str("How are you, Mr. Ananda?"); // Target string to search for std::string target_string("Mr.Ananda"); std::cout << "Is " << target_string << " a substring of " << my_str << " ? "; size_t substring_size = my_str.find(target_string); if (substring_size == 0) std::cout << "No "; else std::cout << "size of matched substring = " << substring_size << std::endl; return 0; }
Output:
Example 2:
#include#include #include void print(int id, std::string::size_type n, std::string const& s) { std::cout << id << ") "; if (std::string::npos == n) std::cout << "not found! n == npos "; else std::cout << "found @ n = " << n << ", substr(" << n << ") = " << std::quoted(s.substr(n)) << ' '; } int main() { std::string::size_type n; std::string const s = "This is a string"; /* ^ ^ ^ 1 4 7 */ Start searching at the beginning of the string n = s.find("is"); print(1, n, s); // search from position 7 n = s.find("is", 7); print(4, n, s); locate one particular character n = s.find('a'); print(7, n, s); locate one particular character n = s.find('q'); print(6, n, s); }
Output:
The string find() function locates a specific substring inside a provided string. It returns the numerical position of the initial character in the Strings substring.
SYNTAX
Consider the strings str1 and str2.
As a result of the SYNTAX: str1.find(str2)
Example:
#includeusing namespace std; // Driver function int main() { // Using Both Strings as Input string str= "hello poojitha"; string str1 = "lo"; // Using the C++ String find() function cout <<" The Position of the string is :"<< str.find(str1); return 0; }
Output:
Determine the occurrence of a character.
The find function can also be used to find the occurrence of a particular character in a string.
Syntax:
size_t find (const char c, size_t pos = 0);
Example:
// C++ application to demonstrate //How to use string find to locate the occurrence of a character #include#include using namespace std; // Driver code int main() { string str = "hello Reema mam"; char c = 'h'; // Find the first occurrence of 'e' size_t found = str.find(c); if (found != string::npos) cout << "First occurrence is " << found << endl; // Find the next occurrence of 'a' found = str.find(c, found + 1); if (found != string::npos) cout << "Next occurrence is " << found << endl; return 0; }
Output:
Look for an incomplete string.
Syntax:
size_t find (const char *str, size_t pos, size_t n);
Example:
// C++ program to demonstrate // working of string find to // search a string #include#include using namespace std; // Driver code int main() { string str = "tutorials point"; // Only search the first 5 characters // of "point" size_t found = str.find("point", 0, 5); if (found != string::npos) cout << found << endl; return 0; }
Output: