Substring in C++
In C++, std::substr() is a predefined function used for string handling. string.h is the header file required for string functions.
This function takes two values pos and len as an argument and returns a newly constructed string object with its value initialized to a copy of a sub-string of this object. Copying of string start from pos and done till pos+len means [pos, pos+len).
Important points:
- The index of the first character is 0 (not 1).
- If pos is equal to the string length, the function returns an empty string.
- If pos is greater than the string length, it throws out_of_range. If this happen, there are no changes in the string.
- If for the requested sub-string len is greater than size of string, then returned sub-string is [pos, size()).
Syntax:
string substr (size_t pos, size_t len) const; Parameters: pos: Position of the first character to be copied. len: Length of the sub-string. size_t: It is an unsigned integral type. Return value: It returns a string object.
// CPP program to illustrate substr() #include <string.h> #include <iostream> using namespace std; int main() { // Take any string string s1 = "Geeks"; // Copy three characters of s1 (starting // from position 1) string r = s1.substr(1, 3); // prints the result cout << "String is: " << r; return 0; } |
Output:
String is: eek
Applications :
- How to get sub-string after a character?
In this a string and a character is given and you have to print the sub-string followed by the given character.
Extract everything after the “:” in the string “dog:cat”.// CPP program to illustrate substr()#include <string.h>#include <iostream>usingnamespacestd;intmain(){// Take any stringstring s ="dog:cat";// Find position of ':' using find()intpos = s.find(":");// Copy substring after posstring sub = s.substr(pos + 1);// prints the resultcout <<"String is: "<< sub;return0;}chevron_rightfilter_noneOutput:
String is: cat
- Print all substrings of a given string
- Sum of all substrings of a string representing a number
- Count occurrences of a substring recursively
- Lexicographical Maximum substring of string
- Print substring of a given string without using any string function and loop in C
- Structures in C++
- Variables in C++
- Implementing Forward Iterator in BST
- Count substrings that contain all vowels | SET 2
- Machine Learning in C++
- How can we use Comma operator in place of curly braces?
- Basic Code Optimizations in C
- Optimally accommodate 0s and 1s from a Binary String into K buckets
- Constants vs Variables in C language
- Sum of an array using MPI
- Find a number containing N - 1 set bits at even positions from the right
This article is contributed by Akash Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



