C++ - C++ Substring Explained - Programming Languages

Our Guide to C++ Substrings

Text processing and manipulation are key to a variety of C++ projects. However, you may not always want to use an entire string of text that’s available through either previous definition or user input. You might instead be looking for a specific keyword or would like to store only a section of a user’s answer. 

In this article, we’ll explore how a substring function can help solve issues that concern only a section of a whole string, before going over some C++ projects in which you might find substrings.

What Is a String?

If you’re an aspiring C++ developer, you probably have an understanding of the string as a one-dimensional sequence of characters. Strings are commonly used to manipulate text in a C++ program. Text can also be manipulated through methods like creating arrays of characters to replace strings. Though the output we obtain from these two methods might look the same, the way they gain input and process it is different.

For example, we may define a character array by specifying what we expect to be the maximum length of the user’s input, before outputting it:

#include <iostream>
using namespace std;

int main()
{
    char array[15];

    cout << "What day is it today? ";
    cin >> array;
    cout << "Today is " << array;
   
    return 0;
}

On the other hand, if we were to use a string:

#include <iostream>
using namespace std;

int main()
{
    string myString;

    cout << "What day is it today? ";
    cin >> myString;
    cout << "Today is " << myString;
   
    return 0;
}

If we input “Wednesday” in either case, our output is:

Today is Wednesday

While there are many ways to use and declare strings and char arrays in C++, a key difference from the above examples is that we need to declare the char array length, whereas we don’t need to do so for the string.

Both options are compatible with substrings; today, however, we’ll focus on the string method.

What Is a Substring in C++?

std::substr() is a C++ method that’s used to manipulate text to store a specific part of string. In short, it “extracts” a string from within a string. For instance, “Wednesday” would be the substring of “Today is Wednesday.” 

As we’ll see in the example below, the substring is still declared as a string. What makes it a substring is the function that handles that string:

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    string myString = "Today is Wednesday"; // The main string
    cout << myString << endl; // Outputting the whole string
   
    string theSubString = myString.substr(9,9);
    cout << theSubString; // Outputting only "Wednesday"
   
    return 0;
}

The syntax for using the substring function requires two numerical parameters:

stringName.substr([position], [length])

“Position” here refers to the point at which the substring starts, while length specifies how many letters from the defined position the substring should include. This is where it’s helpful to think about the string as a type of character array, especially since arrays start with 0, which is how the position of a character is calculated in a string.

For example, we could try to get a substring and mark the first position as “1” instead of “0,” like so:

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
  string myString = "Wednesday is here.";
  string subString = myString.substr(1, 9);

    cout << subString;
    return 0;
}

In that case, the output would be:

ednesday

However, that’s not the case for the length parameter, where the count starts from 1. The letter you determine as the position parameter is the first letter in the length you assign.

Both conditions can include operatives to shift the position desired. For example:

string subString = myString.substr(0+2, 5);

We’ll use this knowledge below.

The C++ Substring in Action

So when should you use the substring function in C++?

Say you’re grading a student’s test and they have submitted a massive text file including the test questions and their responses as “true” or “false,” and explanatory paragraphs supporting why they believe their answers to be correct. A small section of that file would look like this:

A scarlet macaw is the largest endangered parrot species - False. The actual largest parrot is the hyacinth macaw. Their blue feathers are gorgeous. There are 11 types of coffee in the world - False. My local store sells a lot more different coffee options, so I'm sure there are more. The largest mountain in the world is Mount Everest - True. It's huge! I saw it in person and I felt like an ant.

Having to deal with this much information can get overwhelming. However, you need to get through your task quickly and simply want to compare the true/false answers to the answer sheet. In the brief section you looked over, you noticed that each answer that really interests you is preceded by a hyphen.

Accordingly, you can write a program to print out each word that follows a hyphen:

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    string studentText = "A scarlet macaw is the largest endangered parrot species - False. The actual largest parrot is the hyacinth macaw. Their blue feathers are gorgeous."; // The main string
   
    int position = studentText.find("-"); // Using the find() function to determine the point we would like to start displaying from
   
    string answer = studentText.substr(position + 2, 5); // Create substring from the found position
    cout << answer;
   
    return 0;
}

Helpfully, the program extracts only the information we need from our pile of text, by displaying True or False.

Note that the substring has to have its starting position shifted by 2 characters from the end of the preceding sentence. Otherwise, it would also include the hyphen and the space. The method works on a single sentence; however, it would also work for a whole file in C++.

The substring length was determined by the longest word in this case: “false.” “True” contains 4 letters; however, the 5th character would simply be a period, which does not impact readability and allows us to keep our code simple.

You can also use the substring function to determine whether a string actually contains a specific substring you’re looking for.

For example, if you’re suspicious that a friend is sharing a secret code or a specific keyword with someone who is not supposed to see it, you can look for it in one of their cryptic messages by having your program check if it contains the word.

Here’s how you would go about doing so:

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    string message = "Yes, I won last round. Bring red over for a hockey game. You can have my secret code after you give me a puppy."; // The message you obtained
    string lookingFor = "secret code"; // The keyword you need to watch out for
   
    if(message.find(lookingFor != string::npos)){
        cout << "Oh no! The secret has been compromised!";
        }
    else{
        cout << "All good!";
        }
   
    return 0;
}

By using simple if-else statements or other forms of flow control, you can determine the outcome of what happens when the substring is detected. In real-life projects, this can be replaced with an alert.

As you may imagine, on a larger scale the substring technique can play a crucial role in finding correct documentation.

Become a C++ Developer

The substring function, while simple, has great potential when combined with other functions or C++ capabilities. Interested in learning how to manipulate files in C++? What about managing static and dynamic memory?

Look no further than our C++ Nanodegree program, which gives you the opportunity to truly master C++ by working through five real-world projects.

Enroll in our C++ Nanodegree program today!

Start Learning