How to find a Substring in C++ String

In this article, we are going to learn How to find a Substring in C++ String with the help of multiple ways. We are going to use the STL find() and rfind() algorithms of the string container class. So let us start ways to find Substring in a C++ String.

STL string class provides us find() function that can be used to Find Substring in a String in C++. This function is very easy to use as you call this function by using string 1 and pass string 2 which you want to find in string 1 as an argument. So let us understand the find() function and how it can be used in different flavors.

Syntax of find() function

size_t find (const string& str);
size_t find (const string& str, size_t pos = 0);
size_t find (const char* s, size_t pos = 0);

Parameters as:

  • str: The sub-string you want to find.
  • s: The sub-string you want to find, as C style string.
  • pos : The initial position index from where the string search is to begin.

The find() 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 a static member with value as the highest possible for the size_t data structure).

1. STL find() function to Find Substring in String in C++


In this example, we are going to see the first flavors of find() function where we are searching a substring in a string.

#include <iostream>
#include <string>

using namespace std;

int main(){
    string str1 = "This is Devenum.com string tutorial.";
    string str2 = "string";
    string str3 = "list";

    if(str1.find(str2) != string::npos)
	{
        cout << "String1 contains the String 2 as a substring" << endl ;
	}
	else
        cout << "String1 Does Not contains the String 2 as a substring" << endl;

    if(str1.find(str3) != string::npos)
	{
        cout << "String1 contains the String 3 as a substring" << endl;
	}
	else
        cout << "String1 Does Not contains the String 3 as a substring" << endl;

    return 0;
}

Output

String1 contains the String 2 as a substring
String1 Does Not contains the String 3 as a substring

2. STL find()function to Find Substring in a section of String in C++


In this example, we are going to see the second flavor of find() function where we are searching a substring in a string with a range. Here range means the index positions within a string. We can specify the index range from where we need to start and search the sub-string.

#include <iostream>
#include <string>

using namespace std;

int main(){
    string str1 = "This string tutorial is on Devenum.com ";
    string str2 = "list";
	string str3 = "string";
 
    constexpr int Index = 12;

    if(str1.find(str2.c_str(), Index) != string::npos)
	{
        cout <<" We found the sub string str2 in the given range " << endl;
	}
	else
        cout << "We didnt find the sub string str2 in the given range" << endl;
		
	if(str1.find(str3.c_str(), Index) != string::npos)
	{
        cout <<" We found the sub string str3 in the given range " << endl;
	}
	else
        cout << "We didnt find the sub string str3 in the given range" << endl;

    return 0;
}

Output

We didnt find the sub string str2 in the given range
We didnt find the sub string str3 in the given range

3. STL find()function to Find character in String in C++


In this example, we are going to learn how to use the find() function to search for a character in a string.

#include <string>

using namespace std;

int main(){
    string str1 = "This string tutorial is on Devenum.com ";
    char ch2 = 's';
	char ch3 = 'p';

    if(str1.find(ch2) != string::npos)
	{
        cout <<"We found the Character in the string " << endl;
	}
	else
        cout << "We didnt find the Character in the string" << endl;
		
	if(str1.find(ch3) != string::npos)
	{
        cout <<"We found found the Character in the string " <<endl;
	}
	else
        cout << "We didnt find the Character in the string" << endl;

    return 0;
}

Output

We found the Character in the string 
We didnt find the Character in the string