Determining the Presence of a Substring in a String in C
In C , a common task programmers encounter is checking if a string contains another string. To accomplish this, several methods are available.
One efficient method is to utilize the std::string::find function. This function returns the position of the first occurrence of the specified substring within the string. If the substring is absent, std::string::npos is returned, a special value indicating that the substring is not present.
To check if a string contains a substring, you can employ the following steps:
Here's an example:
if (s1.find(s2) != std::string::npos) { std::cout << "found!" << '\n'; }
In this code, if s2 is a substring of s1, "found!" will be printed. Remember, both s1 and s2 must be of type std::string.
The above is the detailed content of How Can I Check if a String Contains a Substring in C ?. For more information, please follow other related articles on the PHP Chinese website!