What is the function of the C++ substr function?

The substr function in C++ is used to extract a substring from a string. It starts at a specified position, cuts out a portion of the string, and returns that substring.

The syntax for the substr function is:
string substr (size_t pos, size_t len) const;

pos is the position to start extracting from, and len is the length of the substring to extract. If the len parameter is omitted, it will default to extracting all characters from pos to the end of the string.

For example, with the string “Hello, World!”, we can use the substr function to extract a sub-string as follows:
string str = “Hello, World!”;
string sub = str.substr(7, 5);

The code above extracts a substring of length 5 starting from position 7 in the string ‘str’, which is “World”. The resulting substring will be stored in the variable ‘sub’.

bannerAds