Introduction to C++ Substring
A part of the string is called substring in C++ and if we want to retrieve a substring from a given string in C++, we make use of a function called substr() function.It takes the two parameters position and length where position represents the starting position of the substring in the given string and length represents the number of characters in the substring to be retrieved from the given string.This substr() function returns the substring extracted from the given string starting from the specified position up to the number of characters from the starting position specified as length.
Syntax:
substr(position, length)
Where position represents the starting position of the substring in the given string and length represents the number of characters in the substring to be retrieved from the given string.
Working of Substr() Function in C++
Working of substr() function in C++ is as follows:
- A part of the string is called substring in C++ and if we want to retrieve a substring from a given string in C++, we make use of a function called substr() function.
- The substr() function takes the two parameters namely position and length.
- The parameter position represents the starting position of the substring in the given string.
- The parameter length represents the number of characters in the substring to be retrieved from the given string.
- The substr() function returns the substring extracted from the given string starting from the specified position up to the number of characters from the starting position specified as length.
Examples of C++ Substring
Following are the examples are given below:
Example #1
C++ program to demonstrate substr function that returns the substring extracted from the given string starting from the specified position up to the number of characters from the starting position specified as length:
Code:
//the two headers iostream and string are included to be able to make use of cin, cout and substr functions
#include <iostream>
#include <string.h>
using namespace std;
//main method is called
int main()
{
//a string variable called strone is defined to store a string from which the substring is to be extracted
string strone = "Welcome to C++_learning";
//substr function is used to extract the substring from the given string starting from the specified position upto the specified length and the resulting substring is stored in a string variable called strtwo
string strtwo = strone.substr(11, 12);
cout << "The given string is: " << strone << "\n" <<endl;
//displaying the extracted substring
cout << "The substring extracted from the given string is: " << strtwo << "\n" << endl;
return 0;
}
Output:
In the above program, we have included the headers iostream and string which allows us to make use of the functions cin, cout and substr. Then the main method is called within which a string variable called strone is defined to store the string from which the substring is to be extracted. Then the substr function is used to extract the substring from the given string strone starting from the specified position upto the specified length and the resulting substring is stored in a string variable called strtwo. Then the extracted substring stored in the string variable called strtwo is displayed as the output on the screen.
Example #2
C++ program to demonstrate substr function that returns the substring extracted from the given string starting from the specified position up to the number of characters from the starting position specified as length:

4.5 (8,820 ratings)
View Course
Code:
//the two headers iostream and string are included to be able to make use of cin, cout and substr functions
#include <iostream>
#include <string.h>
using namespace std;
//main method is called
int main()
{
//a string variable called strone is defined to store a string from which the substring is to be extracted
string strone = " EDUCBA is the best site for learning";
//substr function is used to extract the substring from the given string starting from the specified position upto the specified length and the resulting substring is stored in a string variable called strtwo
string strtwo = strone.substr(0, 6);
cout << "The given string is: " << strone << "\n" <<endl;
//displaying the extracted substring
cout << "The substring extracted from the given string is: " << strtwo << "\n" << endl;
return 0;
}
Output:
In the above program, we have included the headers iostream and string which allows us to make use of the functions cin, cout and substr. Then the main method is called within which a string variable called strone is defined to store the string from which the substring is to be extracted. Then the substr function is used to extract the substring from the given string strone starting from the specified position upto the specified length and the resulting substring is stored in a string variable called strtwo. Then the extracted substring stored in the string variable called strtwo is displayed as the output on the screen.
Example #3
C++ program to demonstrate substr function that returns the substring extracted from the given string starting from the specified position up to the number of characters from the starting position specified as length:
Code:
//the two headers iostream and string are included to be able to make use of cin, cout and substr functions
#include <iostream>
#include <string.h>
using namespace std;
//main method is called
int main()
{
//a string variable called strone is defined to store a string from which the substring is to be extracted
string strone = " Learning is fun";
//substr function is used to extract the substring from the given string starting from the specified position upto the specified length and the resulting substring is stored in a string variable called strtwo
string strtwo = strone.substr(12, 3);
cout << "The given string is: " << strone << "\n" <<endl;
//displaying the extracted substring
cout << "The substring extracted from the given string is: " << strtwo << "\n" << endl;
return 0;
}
Output:
In the above program, we have included the headers iostream and string which allows us to make use of the functions cin, cout and substr. Then the main method is called within which a string variable called strone is defined to store the string from which the substring is to be extracted. Then the substr function is used to extract the substring from the given string strone starting from the specified position upto the specified length and the resulting substring is stored in a string variable called strtwo. Then the extracted substring stored in the string variable called strtwo is displayed as the output on the screen.
Recommended Articles
This is a guide to C++ Substring. Here we also discuss the introduction and working of substr() function in c++ along with different examples and its code implementation. You may also have a look at the following articles to learn more –