C++ Substring

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

In this tutorial, we go through some of the processes to find a substring of a given string, given a starting position, length or an ending position.

Syntax of substr() function

substr() is a member function of string class. Following is the syntax of substr() function.

substr (pos, len)

substr() function is called on a string object.

pos is the starting position or index of substring in this string.

len is the number of characters in the substring.

The default value of pos is zero. If pos is not specified, then the whole string is returned as 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.

Example – substr() – 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

#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.

Example – substr() – 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

#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.

Example – substr() – 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

#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.

Example – substr() – 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

#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

Example – 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

#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.

Example – substr() – 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

#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.