In C++, the string substr() function is used to extract a substring from the given string. It generates a new string with its value initialized to a copy of a sub-string of the given string.
C++ #include <iostream> #include <string> using namespace std; int main() { // Take any string string s = "Geeks"; // Extract two characters of s1 (starting // from index 3) string sub = s.substr(3, 2); cout << sub; return 0; }
In the above example, we have extracted the last two characters from the string s and stored them in sub string using substr() function.
Syntax of String substr()
The std::string::substr() is the member function of string class defined inside <string> header file.
C++
Parameters:
- pos: Index of the first character to be copied.
- len: Length of the sub-string.
Return Value:
- It returns a string object.
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 happens, there are no changes in the string.
- If the requested sub-string len is greater than the size of a string, then returned sub-string is [pos, size()).
- If len is not passed as a parameter, then returned sub-string is [pos, size()).
Examples of substr()
The following examples illustrates the use of substr() function in C++:
Get a Sub-String after a Character
In this example, a string and a character are given and you have to print the complete sub-string followed by the given character.
C++ #include <iostream> #include <string> using namespace std; int main() { string s = "dog:cat"; // Find position of ':' using find() int pos = s.find(":"); // Extract substring after pos string sub = s.substr(pos + 1); cout << "Substring is: " << sub; return 0; }
Get a SubString Before a Character
This example is a follow-up to the previous one, where we example, we print the complete substring up to a specific character.
C++ #include <iostream> #include <string> using namespace std; int main() { string s = "dog:cat"; // Find position of ':' using find() int pos = s.find(":"); // Copy substring before pos // Extract everything before the ":" in the string // "dog:cat". string sub = s.substr(0, pos); cout << "Substring is: " << sub; return 0; }
Print all Sub-Strings of a Given String
This example is a very famous interview question also. We need to write a program that will print all non-empty substrings of that given string.
C++ #include <bits/stdc++.h> using namespace std; int main() { string s = "abcd"; // subString(s, s.length()); int n = s.length(); // Logic to print all substring // using substr() for (int i = 0; i < n; i++) for (int len = 1; len <= n - i; len++) cout << s.substr(i, len) << endl; return 0; }
Outputa ab abc abcd b bc bcd c cd d
Print Sum of all Substrings of a String Representing a Number
Given an integer represented as a string, we need to get the sum of all possible substrings of this string.
C++ #include <bits/stdc++.h> using namespace std; int main() { string s = "1234"; int res = 0; int n = s.length(); for (int i = 0; i < n; i++) { for (int len = 1; len <= n - i; len++) { string sub = (s.substr(i, len)); // Convert the substring into // integer number int x = stoi(sub); res += x; } } cout << res; return 0; }
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems
My Profile