In this C++ tutorial, you will learn how to find the substring of a given string, using std::string::substr() function, with some examples.

C++ Substring

To find the substring in a string, you can use substr() function, or use a looping technique to find the substring yourself.

Syntax of substr() function

substr() is a member function of std::string class. The following is the syntax of substr() function.

</>
Copy
substr (pos, len)
  • substr() function is called on a string object.
  • pos is the starting position or index of substring in this string. The default value of pos is zero. If pos is not specified, then the whole string is returned as substring.
  • len is the number of characters in the substring. len is optional. If len is not specified, the substring till the end of this string is returned.

substr() returns a newly formed string object representing the substring formed using this string, pos and len.

Examples

1. Find substring with start position and length

In this example, we take a string with some initial value and find the substring of this string starting at given starting position and spanning a specified length.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   string str1 = "We are changing the world.";
   
   int pos = 5;
   int len = 10;
   string substring = str1.substr(pos, len);

   cout << substring;
}

Output

e changing

Note: Even if you provide a length that is out of the limit of the main string, len would be updated such that substring ends at the end of the original string.

2. Find substring with only start position

In this example, we take a string with some initial value and find the substring of this string starting at given starting position. We do not provide the length of substring. The returned substring should span from start position provided till the end of the this string.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   string str1 = "We are changing the world.";
   
   int pos = 5;
   string substring = str1.substr(pos);

   cout << substring;
}

Output

e changing the world.

3. Find substring with no start position

In this example, we take a string with some initial value and find the substring of this string. We do not provide the start position of the substring. The returned string should be a copy of the original string.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   string str1 = "We are changing the world.";
   
   string substring = str1.substr();

   cout << substring;
}

Output

We are changing the world.

4. Find substring with start position and end position

In this example, we take a string with some initial value and find the substring of this string starting at given starting position and ending at a given ending position.

We know that substr() function does not take end position, but length. So, we shall hack the len argument and form a length value using start and end positions of substring in the original string.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   string str1 = "We are changing the world.";
   
   int start = 5;
   int end = 12;
   string substring = str1.substr(start, end - start + 1);

   cout << substring;
}

Output

e changi

5. Find substring using For loop

In this example, we shall find the substring of a string, given start position and length of the substring, using C++ For Loop.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   string str1 = "We are changing the world.";
   
   int start = 5;
   int len = 10;

   char substring[len+1];

   for(int i=0; i < len; i++) {
      substring[i] = str1[start + i];
   }

   //end string
   substring[len] = '\0';

   cout << substring;
}

Output

e changing

Similarly, you can use C++ While Loop or C++ Do-While Loop to find the substring.

6. Find substring with start position out of range

If you provide a position to substr(), that is out of the range of the index in original string, you will get std::out_of_range thrown in runtime.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   string str1 = "We are changing the world.";
   
   int start = 50;
   int len = 10;

   string substring = str1.substr(start, len);

   cout << substring;
}

Output

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr: __pos (which is 50) > this->size() (which is 26)

Conclusion

In this C++ Tutorial, we learned how to find substring of a string in C++ with the help of example programs.