substr() in C++

Last Updated : 31 Aug, 2026

The substr() function in C++ is used to extract a portion of a string and return it as a new string.

  • It extracts a substring starting from a specified position.
  • The length of the substring can be specified or omitted to extract up to the end of the string.
C++
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s = "Geeks";

    // Extract two characters starting from index 3
    string sub = s.substr(3, 2);

    cout << sub;

    return 0;
}  

Output
ks

Explanation: substr(3, 2) starts at index 3 and extracts 2 characters, resulting in "ks".

Syntax of substr()

std::string::substr() is a member function of the std::string class defined in the <string> header.

string substr(size_t pos = 0, size_t count = npos) const;

Parameters:

  • pos: Position of the first character to extract.
  • count: Number of characters to extract.

Return Value: Returns a new string containing the extracted characters.

Important Points

  • String indexing starts from 0.
  • If pos is equal to the string length, an empty string is returned.
  • If pos is greater than the string length, std::out_of_range is thrown.
  • If count exceeds the remaining characters, all characters from pos to the end are returned.
  • If count is omitted, all characters from pos to the end are returned.

Examples of substr()

Extract Substring After a Character

The following example extracts the part of a string that appears after .

C++
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s = "dog:cat";

    size_t pos = s.find(':');

    string sub = s.substr(pos + 1);

    cout << "Substring is: " << sub;

    return 0;
} 

Output
Substring is: cat

Explanation: It finds the position of : in the string and extracts the substring that appears after it. Here, substr() returns "cat", which is then printed as the output.

Extract Substring Before a Character

The following example extracts the part of the string that appears before .

C++
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s = "dog:cat";

    size_t pos = s.find(':');

    string sub = s.substr(0, pos);

    cout << "Substring is: " << sub;

    return 0;
} 

Output
Substring is: dog

Explanation: It finds the position of : in the string and extracts the substring before it. Here, substr() returns "dog", which is then printed as the output.

The following program uses substr() to generate all non-empty substrings of a string.

C++
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s = "abcd";
    int n = s.length();

    for (int i = 0; i < n; i++)
    {
        for (int len = 1; len <= n - i; len++)
        {
            cout << s.substr(i, len) << endl;
        }
    }

    return 0;
} 

Output
a
ab
abc
abcd
b
bc
bcd
c
cd
d

Explanation: It generates and prints all possible substrings of the string "abcd". It uses nested loops to determine the starting position and length of each substring, while substr() extracts and prints each substring.

Given a string representing a number, the following program calculates the sum of all its non-empty substrings.

C++
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s = "1234";
    int sum = 0;
    int n = s.length();

    for (int i = 0; i < n; i++)
    {
        for (int len = 1; len <= n - i; len++)
        {
            int value = stoi(s.substr(i, len));
            sum += value;
        }
    }

    cout << sum;

    return 0;
} 

Output
1670

Explanation: The program generates every non-empty substring using substr(), converts it to an integer using stoi(), and adds it to sum.

Comment