You are reading the article Working Of Substr() Function In C++ updated in October 2023 on the website Khongconthamnam.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Working Of Substr() Function In C++
Introduction to C++ SubstringIn 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.
Start Your Free Software Development Course
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++ SubstringFollowing are the examples:
Example #1C++ 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:
using namespace std; int main() { string strone = "Welcome to C++_learning"; string strtwo = strone.substr(11, 12); cout << "The given string is: " << strone << "n" <<endl; 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 #2C++ 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:
using namespace std; int main() { string strone = " EDUCBA is the best site for learning"; string strtwo = strone.substr(0, 6); cout << "The given string is: " << strone << "n" <<endl; cout << "The substring extracted from the given string is: " << strtwo << "n" << endl; return 0; }Output:
Example #3C++ 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:
using namespace std; int main() { string strone = " Learning is fun"; string strtwo = strone.substr(12, 3); cout << "The given string is: " << strone << "n" <<endl; 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 ArticlesWe hope that this EDUCBA information on “C++ Substring” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
You're reading Working Of Substr() Function In C++
Update the detailed information about Working Of Substr() Function In C++ on the Khongconthamnam.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!