In C++, string find() is a built-in library function used to find the first occurrence of a substring in the given string. Let’s take a look at a simple example that shows the how to use this function:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "Welcome to GfG!";
string sub = "to";
cout << s.find(sub);
return 0;
}
Explanation: The function returned the starting index of the first occurrence of substring sub in the string s.
Syntax
string find() is a member function of std::string class defined inside <string> header file and have 3 implementations.
C++
s.find(sub, pos); // For substring
s.find(sub, pos, n); // For n character of sub
s.find(c, pos); // For character
The 2nd implementation only works with C style strings.
Parameters
- s: String which is to be searched.
- sub: Substring to search. Can be C++ or C style string.
- pos: Position from where the string search is to begin. By default, it is 0.
- n: Number of characters to match.
Return Value
- Returns the integer representing the index of the first occurrence of the sub-string.
- If the sub-string is not found, it returns string::npos.
To learn how to use the find
function effectively, the C++ Course provides practical examples and detailed explanations.
Examples of string find()
The following examples demonstrate the use of string find() function for different purposes.
Check if Substring Exists
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "Welcome to GfG!";
string sub = "hello";
// Checking if sub is present in s
int res = s.find(sub);
if (res != string::npos)
cout << res;
else
cout << sub << " NOT found.";
return 0;
}
Explanation: In the above program we checked the string s to find the substring sub using the string find() function. As the string s does not contains any instance of string sub the string find() function returns string::npos.
Find Multiple Occurrences of a Substring
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "welcome to geeksforgeeks";
char sub[] = "geeks";
// Loop that runs till string::find()
// returns string::npos
int res = -1;
while ((res = s.find(sub, res + 1)) !=
string::npos)
cout << res << " ";
return 0;
}
Explanation: In the above program a loop is used that runs till string find() returns string::npos and in each iteration, the part of the string where the previous occurrence was found is not considered in the search. In this way the program finds all the occurrences of the string sub withing the string s.
Find All the Occurrences of a Character
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "welcome to GfG";
char c = 'e';
// Loop that runs till string::find()
// returns string::npos
int res = 0;
while ((res = s.find(c, res + 1)) !=
string::npos)
cout << res << " ";
return 0;
}
Explanation: The above program uses a loop that runs till string find() returns string::npos and within the loop a string find function is used which finds the occurrences of the character 'c' in the given string , after each iteration,part of the string where the previous occurrence of the character was found is not considered in the search. In this way the program finds all the occurrences of the character c withing the string s.
Find Occurrence of a Partial Substring
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "welcome to GfG!";
char sub[] = "come to my house";
// Only searching for "come"
int res = s.find(sub, 0, 4);
if (res != string::npos)
cout << res;
return 0;
}
Explanation: The third parameter in the string find() function is used to specify the first n characters of the substring to be matched. we have matched the first 4 characters of the substring in the above program.
Similar Reads
String Functions in C++ A string is referred to as an array of characters. In C++, a stream/sequence of characters is stored in a char array. C++ includes the std::string class that is used to represent strings. It is one of the most fundamental datatypes in C++ and it comes with a huge set of inbuilt functions. In this ar
8 min read
getline (string) in C++ The C++ getline() is a standard library function that is used to read a string or a line from an input stream. It extracts the characters from the input stream and appends it to the given string object until the delimiting character is encountered.Example:C++#include <bits/stdc++.h> using name
3 min read
Strings in C++ In C++, strings are sequences of characters that are used to store words and text. They are also used to store data, such as numbers and other types of information in the form of text. Strings are provided by <string> header file in the form of std::string class.Creating a StringBefore using s
5 min read
C String Functions C language provides various built-in functions that can be used for various operations and manipulations on strings. These string functions make it easier to perform tasks such as string copy, concatenation, comparison, length, etc. The <string.h> header file contains these string functions.Th
6 min read
std::string::insert() in C++ In C++, the string insert() function is used to insert characters or a string at the given position of the string. For example,C++#include <bits/stdc++.h> using namespace std; int main() { string s = "Geeks"; // Inserting another string at the friend // of s s.insert(s.size(), "forGeeks"); cou
4 min read
string at() in C++ The std::string::at() in C++ is a built-in function of std::string class that is used to extract the character from the given index of string. In this article, we will learn how to use string::at() in C++.Syntaxstr.at(idx)Parametersidx: Index at which we have to find the character.Return ValueReturn
1 min read
string erase in C++ The string erase() function is a built-in function of the string class that is used to erase the whole or part of the string, shortening its length. The function provides different ways to delete a portion of a string based on either an index position or a range of characters or delete the whole str
4 min read
std::string::size() in C++ The std::string::size() function in C++ is a built-in method of the std::string class that is used to determine the number of characters in the string. All characters up to the null character are considered while calculating the size of the string.In this article, we will learn about string::size()
2 min read
std::string::data() in C++ The data() function writes the characters of the string into an array. It returns a pointer to the array, obtained from conversion of string to the array. Its Return type is not a valid C-string as no '\0' character gets appended at the end of array. Syntax: const char* data() const; char* is the po
2 min read
std::to_string in C++ In C++, the std::to_string function is used to convert numerical values into the string. It is defined inside <string> header and provides a simple and convenient way to convert numbers of any type to strings.In this article, we will learn how to use std::to_string() in C++.Syntaxstd::to_strin
1 min read