string::substr in C++: Extracting Parts of Strings
Hello! In this article, we’re going to focus on a useful function in C++ meant for working with strings: substr. We’ll begin with an overview of this function, figure out the right ways to use it, and later on, run through some examples to grasp its behavior. By the end, you’ll have a clear picture of the substr function’s importance and utility.
How to Extract a Portion of a String in C++
To take a specific part out of a string in C++, the substr function comes in handy:
string substr(size_t pos = 0, size_t len = npos) const;- The function starts its work from the character at the
posposition. - It grabs
lencharacters from there, or takes characters till the end iflenisn’t specified or if the string is shorter.
Let’s glance at a simple code where we use substr:
#include <iostream>
#include <string>
using namespace std;
int main() {
string sentence = "The world is a beautiful place.";
cout << "Extracted part: " << sentence.substr(4, 5) << endl; // Output: world
return 0;
}In the example above, the program fetches the word “world” from the sentence. Starting from position 4 (remember, we start counting from 0), it extracts 5 characters.
But what if we don’t specify the length or give a value longer than the string’s remaining length?
cout << "Extracted part: " << sentence.substr(4) << endl; // Output: world is a beautiful place.In such cases, substr will simply get everything from the start position till the end of the string.
Important Notes on substr
-
Position and Length: The initial position,
pos, starts at 0 (and not 1). If you provide a position that’s equal to the string’s length, you’ll get an empty string. And, ifpossurpasses the string’s length, you’ll encounter anout_of_rangeexception. -
Type of Variables: The variables
posandlenare ofsize_ttype, which means they can only hold non-negative values. -
Complexity: The time it takes for
substrto work mainly depends on the length of the portion you’re extracting. It’s typically quick, but performance can vary based on the compiler and system. -
Safety: In terms of exceptions, if you do something that the function can’t handle, like asking for a position beyond the string’s length, it will throw an exception. It’ll also throw a
bad_allocexception if it tries to allocate memory but fails.
Real-life Use Case
Imagine you’re working on a program to extract domain names from URLs. Here’s a simplistic approach:
#include <iostream>
#include <string>
using namespace std;
int main() {
string url = "https://www.example.com/page1";
size_t start = url.find("www.");
size_t end = url.find(".com");
cout << "Domain: " << url.substr(start + 4, end - start - 4) << endl; // Output: example
return 0;
}Wrapping Up
The substr function in C++ is incredibly versatile. Whether you’re working on parsing texts, processing user input, or merely chopping down strings, knowing how to use substr can make your programming tasks easier and more efficient.
Remember, while tools like substr are powerful, it’s essential to use them wisely. Always account for possible exceptions and understand the nuances to avoid pitfalls. Happy coding!
Exercises
-
Basic
substrApplication:- Create a C++ program that prompts the user for a sentence.
- Ask the user for a starting position and a length.
- Use the
substrfunction to extract a portion of the user’s sentence based on their input and display the result. - Note: Ensure to handle potential exceptions or errors based on the article’s information.
-
Handling Exceptions:
- Modify the program from the first task.
- Introduce intentional errors, such as providing a position beyond the string’s length.
- Implement exception handling mechanisms to catch any
out_of_rangeorbad_allocexceptions, displaying a relevant error message to the user.
-
Extracting User-defined Domain Names:
- Create a C++ program where the user provides a URL.
- Ask the user to specify the domain’s starting and ending keywords (e.g., “www.” and “.com”).
- Use the
substrfunction, combined with thefindfunction, to extract and display the domain name. - Ensure to handle any potential errors or mismatches in user input.
Discussion