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.

Похожее
vector::push_back in C++: Adding Elements to a Vector
C++ offers various ways to manipulate data. The push_back function is a popular way to add elements to a vector. In this article, we'll delve deep into this function, exploring how and when to use it, and discuss some intriguing aspects related to it.
go

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 pos position.
  • It grabs len characters from there, or takes characters till the end if len isn’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

  1. 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, if pos surpasses the string’s length, you’ll encounter an out_of_range exception.

  2. Type of Variables: The variables pos and len are of size_t type, which means they can only hold non-negative values.

  3. Complexity: The time it takes for substr to 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.

  4. 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_alloc exception 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;
}
Похожее
vector::erase in C++ (With Examples)
C++ offers several ways to manage elements in its containers. The erase function is a popular way to remove elements from the vector. In this article, we'll dive deep into using this function, understand its intricacies, and look at practical examples.
go

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

  1. Basic substr Application:

    • Create a C++ program that prompts the user for a sentence.
    • Ask the user for a starting position and a length.
    • Use the substr function 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.
  2. 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_range or bad_alloc exceptions, displaying a relevant error message to the user.
  3. 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 substr function, combined with the find function, to extract and display the domain name.
    • Ensure to handle any potential errors or mismatches in user input.

Discussion

© 2024, codelessons.dev