Generate Substring – substr()
We can use the substr() function to generate a part of the string as a new string object. It is a member function of the std::string class.
Syntax of substr() in C
str1.substr(start, end);
Parameters
- start: Starting position of the substring to be generated.
- end: Ending of the substring to be generated.
Return Type
- It returns the newly created string object.
Example
std::string text = "Hello, World!"; std::string sub = text.substr(7, 5); // Extract "World"
In the above example. the sub string will contain the “World”.
String Functions In C++
A string is referred to as an array of characters. In C++, a stream/sequence of characters is stored in a char array. C++ includes the std::string class that is used to represent strings. It is one of the most fundamental datatypes in C++ and it comes with a huge set of inbuilt functions. In this article, will look at the functions of string computations.
Contact Us