
C++ Substring Tutorial: How to Extract and Manipulate Strings (with Examples)
Want to master string manipulation in C++? Learn how to use the substr()
function to extract substrings, understand its parameters, and avoid common pitfalls. This guide provides clear explanations and practical examples to improve your C++ skills.
What is substr()
in C++?
The substr()
function in C++ is your tool for extracting portions of a string. It creates a new string that is a copy of a substring from an existing string object. To use substr()
, you need to include the <string>
header file.
This function is useful for various string operations, generating new strings based on parts of an original string.
substr()
Syntax and Parameters Explained
The substr()
function takes two arguments:
string substr (size_t pos, size_t len) const;
- pos: The index of the first character you want to include in your substring. Remember that C++ string indices start at 0.
- len: The length of the substring you want to extract, starting from the
pos
index.
size_t
is an unsigned integer type commonly used for representing sizes and lengths.
Return Value: substr()
returns a new string
object containing the extracted substring.
substr()
Example: Getting a Piece of Your String
Here's a simple example demonstrating how to use substr()
:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "Geeks";
string r = s1.substr(3, 2);
cout << "String is: " << r << endl; // Output: ks
return 0;
}
In this example, we extract a substring of length 2, starting from index 3 of the string "Geeks". The result, "ks", is then printed to the console.
Common substr()
Use Cases and Examples
Let's explore practical examples of how to use the substr()
function effectively.
Beyond the Basics: Advanced substr()
Usage
Here are a few more examples to illustrate different ways to use substr():
s.substr(0, 5)
=> Hello (five letters from the beginning)s.substr(2)
=> llo world (substring from index 2 to the end)s.substr(2, 3)
=> llo (three letters from index 2)
Extracting a Substring After a Specific Character
Use substr()
with find()
to grab everything after a delimiter:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "dog:cat";
int pos = s.find(":");
string sub = s.substr(pos + 1);
cout << "String is: " << sub << endl; // Output: cat
return 0;
}
Getting a Substring Before a Specific Character
Similarly, extract the part of the string before a character:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "dog:cat";
int pos = s.find(":");
string sub = s.substr(0, pos);
cout << "String is: " << sub << endl; // Output: dog
return 0;
}
Important Points to Keep in Mind When Using substr()
- String indices start at 0. The first character is at index 0, not 1.
- If
pos
equals the string length,substr()
returns an empty string. - If
pos
is greater than the string length,substr()
throws anout_of_range
exception. - If
len
is larger than the available characters frompos
to the end, the substring will include characters frompos
to the end of the string. - If
len
is not provided, the substring defaults to characters frompos
to the end of the string.
Applications of Substring in C++
The substr
function is super versatile and useful in many scenarios such as extracting data from a string, like getting a username from an email address, or parsing specific parts of a text file. By using substr
in combination with other string functions like find
, you can easily manipulate string data!