Do you know how strings work in C++? Think again. What if the true nature of strings is not in the whole, but in parts? The C++ substr() function doubles as a powerful tool for unlocking hidden patterns, parsing through complex data, and decoding algorithmic challenges. However, are you using these substrings properly? In this article, you are going to learn the concept of substring in C++, from its basic syntax to real-world applications.
Table of Contents:
What is a Substring in C++? Definition and Use of substr()
A substring in C++ is defined as a subpart of a large string. The substr() function is a member of the std::string class that allows you to extract a substring from a given string, and also specifies the starting position and length of the substring. The substring is part of the std::string class. This member function helps developers with text manipulation and data parsing. This function is a primary way to extract part of a string in C++. Also, the substr() is a versatile member of the C++ string functions.
Syntax:
string substr(size_t pos = 0, size_t len = npos) const;
The C++ substr() function returns a portion of the new string from the original string. Pos parameter specifies the starting index of the first character included in the substring. The len parameter specifies the number of characters to be extracted from the original string. Size_t is an unsigned integral type. This is how you can extract part of a string in C++.
Parameters of substr() Function in C++
The C++ substr() function takes two parameters:
- pos: This parameter defines the starting position of the string and must be within the bounds of the original string.
- pos == string.length(), substr() return an empty string.
- pos > string.length, substr() function throws an out-of-range error.
- len: it defines the length of the string; string::npos is the default value of len, i.e., till the end of the string.
C++ Substring Example:
Output:
The above C++ program uses the substr() function for extracting a specified part of a substring. Here, the string “Hello, World!” is the original string, using text.substr(7, 5) to obtain the substring from the starting index at 7, spanning up to 5 characters. This results in getting the substring “World”. This is a direct example of how to extract part of a string in C++.
Time and Space Complexity of C++ substr()
The time and space complexity of C++ substr() fucntion(from the std::string class) is:
- Time Complexity: O(k), where k is the length of the substring being copied.
- Space Complexity: O(k), because it creates and returns a new std::string object containing the substring.
So, the longer the substring you extract, the more time and memory it will use.
How substr() Works Internally in C++
We use the substr() function to extract a substring in C++ from the original string. When substr(pos, len) is called from the pos index in the original string, it first starts copying characters. The len parameter specifies the number of characters to obtain from a specified string. The C++ substr() function returns the substring that is extracted from the original string by starting at the specified position and continuing for the given number of characters.
Return Value and Output of substr() in C++
The C++ substr() function returns a new string that is obtained from the extracted substring in C++. The return values are mostly stored in another string or may be used directly in expressions.
Examples of Using substr() Function in C++
Let’s check some C++ substring examples that use the substr() function:
1. Substring After a Character
In C++, the “substring after a character” extracts the portion of the string that comes after a specific character. By using the find(), you can easily find the position of the character, and following the substr(), you can get the starting substr just after it. This is essential for tasks like email parsing, URLs, etc, and is a common technique in string manipulation in C++.
C++ Substring Example:
Output:
The above C++ program demonstrates the usage of substr with find(). Here, find() is used to locate the ‘@’ in the string, then it uses the substr() function to get everything after it.
2. Substring Before a Character
Here, we will define the string that is before the special character. In C++, you can use the find() function to obtain the position of that character and then apply substr(0, pos) to retrieve everything before it. This is often used to substring the username from an email address.
C++ Substring Example:
Output:
The above C++ program extracts the portion that is before the ‘@’ symbol. It first uses the find() to locate the ‘@’ and uses the substr(0, pos) function to get the actual username. The final result, “user,” is printed on your console.
3. How to Generate All Substrings in C++
It generates and prints all possible substrings of the given string. In C++, this is often accomplished with two nested loops: The first for positioning the beginning and the latter for setting the length. This technique is useful for searching, analyzing, and matching patterns within a string. This is another form of string slicing in C++.
C++ Substring Example:
Output:
This C++ code will print all the substrings of the string abc. It utilises two nested loops: the outer loop (i), where the starting index of the substring is set, and the inner loop (j), where the length of the substring starting from that index is fixed. We use the substr(i,j) function to create and print every substring. For example, for “abc”, the output would be: “a”, “ab”, “abc”, ”b”, “bc”, “c”.
4. Get Maximum and Minimum Values of All Substrings Representing a Number
This approach refers to extracting all valid substrings from a given string of digits and returning the largest and smallest ones. An approach for this in C++ is to loop through all possible substrings, converting to integers using stoi() and comparing the values to keep track of the minimum and maximum. It comes in handy when dealing with problems involving numeric analysis or pattern identification.
C++ Substring Example:
Output:
This C++ program extracts the maximum and minimum numeric values from all substrings of the string “8347.” It uses nested loops to generate each possible substring, converts it to an integer using stoi(), and then updates the max and min values. As an output, it displays the maximum and minimum formed by the digits of any string.
Detecting Overlapping Substrings with substr() in C++
In a string, overlapping substrings occur more than once in the same string and might share some characters. In contrast to simple repetition, overlapping means that the end of one instance may overlap the beginning of another.
Explanation:
- Accepts a primary string and a target substring.
- Iterates over the primary string with a loop.
- Compare at each index a substring of length equal to the target.
- Advances the index 1 position to enable overlapping.
C++ Substring Example:
Output:
The sample input is a text and a pattern, and we have to find the overlapping substrings in the text; This pattern would solve the problem for us. If so, it increments the counter. In the “ababab” string, “ab” was matched at positions (0, 2, 4), and the return count is 3. The program then prints the total count.
Real-World Applications of C++ substr() Function
- CSV or TSV parsing – A common pattern in most data processing, where strings are split by commas or tabs, and separate fields need to be extracted. This is a common task when you need to extract part of a string in C++.
- Log File Analysis – Parse structured log lines to get timestamps, log levels, and messages.
- Often used to generate diagnostics, summaries, or shortened forms of longer strings to display in user interfaces. This is a practical application of string manipulation in C++.
- Data Masking – Using splitting and substitution at a certain part of sensitive values, like credit card numbers or phone numbers.
- Longest Palindromic Substring - Determines if a given substring is a palindrome.
- Sliding Window Technique – In a loop, it creates fixed-size substrings useful for problems concerning pattern searches or hashing. This is another application of string slicing in C++.
Difference Between substr() and find() in C++
Aspect |
substr() in C++ |
find() in C++ |
Purpose |
Extracts a portion of the string |
Searches for a substring within a string |
Return Type |
Returns a new std::string |
Returns size_t (position index) or std::string::npos if not found |
Parameters |
start index, length |
substring to search, optional start index |
Modifies String? |
No, it just creates and returns a new substring |
No, it only searches in the original string |
Common Use Case |
To get a part of a string, like a word or date |
To check if a word exists or find its position |
Error Handling |
May throw out_of_range if the index is invalid |
Returns npos if the substring is not found |
Time Complexity |
O(k), where k is the length of the substring |
O(n), where n is the length of the original string |
Example |
“hello”.substr(1, 3) returns “ell” |
“hello world”.find(“world”) returns 6 |
Output Format |
A new string |
A numeric position index |
Handling Out-of-Range Errors in substr()
- The substr() function throws an out_of_range error if the start index is greater than the string length.
- You can catch this error using a try-catch block.
- Use catch (const std::out_of_range& e) to handle the exception safely.
- Always check that the starting index is less than the string’s size before calling substr().
- This prevents your program from crashing unexpectedly.
- You can also use str.length() or str.size() to verify valid index ranges.
- Writing code carefully helps keep your program safe and bug-free. This is important for all C++ string functions.
- It is a good practice to handle possible exceptions when working with strings.
Common Mistakes When Using C++ substr() Function
1. If you use a start index that’s bigger than the string’s length, the program will throw an error.
Example: “hello”.substr(10, 2); will crash.
2. substr() does not change the original string; it only returns a new part of it.
Example: After s.substr(1, 2);, the string s stays the same.
3. Not checking the string’s length before using substr() can cause errors. Always make sure the starting index is valid with something like if (start < str.length()).
4. Using negative numbers for start or length gives unexpected results because C++ treats them as large positive numbers.
Example: substr(-1, 3); won’t work as expected.
5. Some people confuse the second number in substr() as the end position, but it actually means how many characters to take.
Example: substr(2, 5) starts at index 2 and takes 5 characters.
6. If you don’t store or use the result of substr(), then the result gets lost.
Example: Just writing str.substr(0, 4); does nothing unless saved.
7. substr() only throws an error if the start index is too big, not if the length is too long. If the length goes beyond the end of the string, it just returns as much as it can.
8. Using fixed numbers without checking the string size can break your program. Always check the string’s length to make your code safe for any input.
Conclusion
String handling is an important feature in C++, and the substr() function is a great way to create a substring in C++, a subset of your string. Use cases like data parsing, text processing, and algorithmic problems. A substr() simplifies and optimises string manipulation in C++, ranging from simple string slicing in C++ to complex tasks, such as log analysis and pattern matching. Mastering substr() is essential for efficient string manipulation in C++ applications.
These articles offer a thorough overview of the core principles and basics of C++.
C++ assignment operators – Covers basic and compound assignment syntax.
Pass objects to functions in C++ – Covers pass by value, reference, and pointer.
Why are not variable length arrays part of the C++ standard? – Discusses memory safety and compile-time sizing.
Getline in C++ – Demonstrates reading full lines including spaces.
Array decay or array to pointer conversion in C++ – Explains “decay” during function calls.
Read binary files in C++ – Uses ifstream with ios::binary mode.
ifstream get in C++ – Reads one character at a time from streams.
istream readsome in C++ – Reads characters already buffered in stream.
Substring in C++ – FAQs
Q1. What is a substring in C++?
A substring in C++ is a portion of a string extracted using the `substr()` function.
Q2. What does the substr() function do in C++?
It copies part of a string starting at a given position, with an optional length.
Q3. What happens if the length parameter is not provided in substr()?
The substring will include all the characters from the start index to the end of the string.
Q4. Can substr() throw an exception in C++?
Yes, it can throw std::out_of_range when the start index is greater than the string length.
Q5. Is substr() zero-based in indexing?
Yes, substr() uses zero-based indexing for the start position.
Q6. Can substr() be used with string literals directly?
No, substr() cannot be called directly on a string literal. You must first convert it to an std::string.
Q7. How to extract a substring in C++?
You can extract a substring in C++ using the substr(start_index, length) method of the std::string class.
Q8. What is string manipulation in C++?
String manipulation in C++ refers to performing operations like modifying, searching, extracting, or formatting text using std::string and related functions.
Q9. How can you perform string slicing in C++ using the substr() function?
You can perform string slicing in C++ using the substr(start, length) function to extract a part of the string.
Q10. What are some commonly used C++ string functions for manipulating and analyzing text?
Commonly used C++ string functions are length(), substr(), find(), replace(), append(), and compare().