In this C++ tutorial, you will learn how to check if a given string starts with a specific prefix value, with example programs.

Check if the string starts with a specific prefix in C++

C++ Check if string starts with a specific prefix value

To check if the string starts with a specific prefix value in C++, get the substring of the given string that starts at index=0 and spans a length as that of the specified prefix value, and then compare it to the specified prefix value.

If the substring equals the prefix value, then we can say that the given string starts with the specified prefix value. Otherwise the given string does not start with the specified value.

1. Checking if the string starts with a specific prefix using substring and string equality in C++

In the following program, we take a string in str and a prefix string value in prefix. We have to check if the string str starts with the prefix string prefix using string substr() method and string equality check.

Steps

  1. Given an input string in str, and a prefix string in prefix.
  2. Call the substr() method of the string class to extract a substring of str starting from index 0 and having a length equal to the length of prefix. Store the extracted substring in variable substr.
  3. Then compare the substring with the prefix string using the == operator.
  4. If the substring equals the prefix, then the string str starts with the specified prefix value. Else the string str does not start with the specified prefix value. You can use C++ If Else statement to implement this step, with substr == prefix as the if-condition.

Program

main.cpp

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

int main() {
    string str = "Hello World";
    string prefix = "Hello";

    if (str.substr(0, prefix.length()) == prefix) {
        cout << "The string starts with the prefix." << endl;
    } else {
        cout << "The string does not start with the prefix." << endl;
    }

    return 0;
}

Since the given string in str starts with the specified prefix string, the if-condition evaluates to true, and the if-block runs.

Output

The string starts with the prefix.

Now, let us change the prefix value, and run the program again.

main.cpp

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

int main() {
    string str = "Hello World";
    string prefix = "Apple";

    if (str.substr(0, prefix.length()) == prefix) {
        cout << "The string starts with the prefix." << endl;
    } else {
        cout << "The string does not start with the prefix." << endl;
    }

    return 0;
}

Since the given string in str does not start with the specified prefix string, the if-condition evaluates to false, and the else-block runs.

Output

The string does not start with the prefix.
ADVERTISEMENT

2. Checking if the string starts with a specific prefix using string compare() method in C++

In the following program, we take a string in str and a prefix string value in prefix. We have to check if the string str starts with the prefix string prefix using string compare() method.

Steps

  1. Given an input string in str, and a prefix string in prefix.
  2. Use the string compare() method to compare the substring of str starting from index 0 and having a length equal to the length of prefix with the prefix string.
  3. If the value returned by compare() method is 0, then the string str starts with the specified prefix value. Else the string str does not start with the specified prefix value.

Program

main.cpp

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

int main() {
    string str = "Hello World";
    string prefix = "Hello";

    if (str.compare(0, prefix.length(), prefix) == 0) {
        cout << "The string starts with the prefix." << endl;
    } else {
        cout << "The string does not start with the prefix." << endl;
    }

    return 0;
}

Output

The string starts with the prefix.

Conclusion

In this C++ Tutorial, we learned how to check if the string starts with the specified prefix value using string compare() method, or string substr() method and string equality check, with examples.