Updated June 16, 2023
Introduction to C++ Substring
In C++, a substring refers to a part of a string. To retrieve a substring from a given string in C++, the substr() function is used. 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 retrieve from the given string.
Working of Substr() Function in C++
Working of substr() function in C++ is as follows:
- In C++, we use the substr() function to retrieve a substring from a given string. A substring in C++ refers to a part of the string.
- The substr() function takes two parameters: 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 retrieve 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:
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, allowing us to use cin, court, and substr. The program executes the primary method and defines a string variable, “strone,” to store the original string from which a substring will be extracted. Using the substr function, the program extracts a substring from the string “strone,” starting from a specified position and extending for a specified length. The resulting substring is stored in the string variable “strtwo.” Finally, the program displays the extracted substring, which is stored in the variable “strtwo,” 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:
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, allowing us to use cin, court, and substr. The program executes the main method, which defines a string variable named “strone” to store the original string. It then utilizes the substr function to extract a substring from “strone” starting from a specified position and extending for a specified length. A string variable named “strtwo” stores the resulting substring. Finally, the program displays the output on the screen as the extracted substring stored in the “strtwo” variable.
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, allowing us to use cin, court, and substr. The program calls the main method and defines a string variable called “strone” to store the original string. The program then uses the substr function to extract a substring from the string “strone” starting from a specified position and extending for a specified length. It stores the resulting substring in the string variable “strtwo.” Finally, the program displays the extracted substring, which is stored in the variable “strtwo,” as the output on the screen.
Recommended Articles
We hope that this EDUCBA information on “C++ Substring” was beneficial to you. You can view EDUCBA’s recommended articles for more information.