How to use the string find() in C++?
C++ is a versatile and powerful programming language that offers a wide range of built-in functions to help developers manipulate strings. One such function is find(), which is used to search for a specific substring within a larger string. In this blog post, we'll take a deep dive into find() and explore its syntax, usage, and examples.
Syntax of find()
The find() function is a member of the string class in C++. It has the following syntax:
string::size_type find (const string& str, size_type pos = 0) const noexcept;
Let's break down this syntax into its component parts:
- string::size_type is a data type that represents the size of a string. It is an unsigned integer type.
- find() is the name of the function.
- (const string& str) is the parameter of the function. It represents the substring that we want to search for within the larger string.
- size_type pos = 0 is an optional parameter that represents the position in the larger string where we want to begin our search. If we don't specify a value for this parameter, the function will start searching from the beginning of the string.
- const noexcept is an optional exception specification that indicates that the function will not throw any exceptions.
Usage of find()
The find()function is used to search for a specific substring within a larger string. It returns the index of the first occurrence of the substring in the larger string. If the substring is not found, the function returns a special value called string::npos.
Example:
Let's see an example of how to use find():
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, world!";
std::string sub = "world";
std::size_t found = str.find(sub);
if (found != std::string::npos) {
std::cout<< "Substring found at index " << found <<std::endl;
} else {
std::cout<< "Substring not found" <<std::endl;
}
return 0;
}
In this example, we have a larger string str that contains the substring "world". We use the find() function to search for the substring within the larger string, and we store the result in the variable found. If the substring is found, we print out its index. If the substring is not found, we print out a message indicating that.
Output:
Substring found at index 7
In this example, the find() function returns the value 7, which is the index of the first occurrence of the substring "world" within the larger string "Hello, world!".
Example:
Let's look at another example:
#include <iostream>
#include <string>
int main() {
std::string str = "The quick brown fox jumps over the lazy dog";
std::string sub = "cat";
std::size_t found = str.find(sub);
if (found != std::string::npos) {
std::cout<< "Substring found at index " << found <<std::endl;
} else {
std::cout<< "Substring not found" <<std::endl;
}
return 0;
}
In this example, we have a larger string str that does not contain the substring "cat". We use the find() function to search for the substring within the larger string, and we store the result in the variable found. Since the substring is not found, the find() function returns the special value string::npos.
Output:
Substring not found
In this example, the find() function returns the special value string::npos, indicating that the substring "cat" is not found within the larger string "The quick brown fox jumps over the lazy dog".
It's worth noting that the find() function is case-sensitive. It means that if we search for a substring in all lowercase letters, it will not match with any uppercase letters in the larger string.
For example:
#include <iostream>
#include <string>
int main() {
std::string str = "The quick brown fox jumps over the lazy dog";
std::string sub = "FOX";
std::size_t found = str.find(sub);
if (found != std::string::npos) {
std::cout<< "Substring found at index " << found <<std::endl;
} else {
std::cout<< "Substring not found" <<std::endl;
}
return 0;
}
Output:
Substring not found
In this example, the find()function is searching for the substring "FOX" within the larger string "The quick brown fox jumps over the lazy dog". However, since the function is case-sensitive and the substring "FOX" contains uppercase letters, it is not found in the larger string.