Substring in C++

Substring in C++ is achieved using str::substr() which is a predefined function comes with string.h header file. This function basically takes two arguments pos and len. In this function the index starts from 0.

Basic Syntax

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

And the parameters are:

ParameterDescription
posThe position from which the substring should start.
lenLength of the substring that should will be returned. Means the last index of the string is pos+len
out[Optional] Alternate output array in which to place the result.

Return Value

This function will return string object which is a substring of given string.

Examples

Following are the examples for str::substr() method.

Example 1

The output should be:

String str2 : I love
String str3 : love crazygeeks.org

Example 2

The output should be:

String is: Website=CrazyGeeks.ORG

Substring is: CrazyGeeks.ORG

LEAVE A REPLY

Please enter your comment!
Please enter your name here