This post will discuss how to check if a string starts with a certain string in C++.
1. Using string::rfind
The string::rfind
function searches the string for the last occurrence of the specified string, starting at or before the specified position. To check whether a string starts with the specified string, pass position 0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> int main() { std::string str = "C++17"; std::string prefix = "C++"; if (str.rfind(prefix, 0) == 0) { std::cout << "String starts with the given prefix" << std::endl; } else { std::cout << "String doesn't starts with prefix" << std::endl; } return 0; } |
Output:
String starts with the given prefix
2. Using string::compare
The string::compare
function compares the value of a string with the specified sequence of characters. A zero status code indicates the strings are equal. It can be used as follows to match against a prefix:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> bool startsWith(const std::string &str, const std::string &prefix) { return str.size() >= prefix.size() && str.compare(0, prefix.size(), prefix) == 0; } int main() { std::string str = "C++17"; std::string prefix = "C++"; if (startsWith(str, prefix)) { std::cout << "String starts with the given prefix" << std::endl; } else { std::cout << "String doesn't starts with prefix" << std::endl; } return 0; } |
Output:
String starts with the given prefix
3. Using Boost
If you already use boost library in your project, consider using the boost::algorithm::starts_with
function from header <boost/algorithm/string/predicate.hpp>
. It returns true
if the input starts with the prefix.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <boost/algorithm/string/predicate.hpp> int main() { std::string str = "C++17"; std::string prefix = "C++"; if (boost::starts_with(str, prefix)) { std::cout << "String starts with the given prefix" << std::endl; } else { std::cout << "String doesn't starts with prefix" << std::endl; } return 0; } |
Output:
String starts with the given prefix
4. Using string::substr
The string::substr
function returns a substring of a string between specified positions. It can be used as follows to match a string against a prefix:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> bool startsWith(const std::string &str, const std::string &prefix) { return str.size() >= prefix.size() && str.substr(0, prefix.size()) == prefix; } int main() { std::string str = "C++17"; std::string prefix = "C++"; if (startsWith(str, prefix)) { std::cout << "String starts with the given prefix" << std::endl; } else { std::cout << "String doesn't starts with prefix" << std::endl; } return 0; } |
Output:
String starts with the given prefix
5. Using string::starts_with
Starting with C++20, we can use the string::starts_with
function, which returns true
if the string starts with the specified prefix. This is demonstrated below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> int main() { std::string str = "C++17"; std::string prefix = "C++"; if (str.starts_with(prefix)) { std::cout << "String starts with the given prefix" << std::endl; } else { std::cout << "String doesn't starts with prefix" << std::endl; } return 0; } |
Output:
String starts with the given prefix
6. Using std::mismatch
Finally, the STL library offers the std::mismatch algorithm, which compares the elements in two specified ranges and returns a pair specifying the first position where they differ. It can be used as follows, to check if the string starts with a certain string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <algorithm> int main() { std::string str = "C++17"; std::string prefix = "C++"; if (std::mismatch(prefix.begin(), prefix.end(), str.begin()).first == prefix.end()) { std::cout << "String starts with the given prefix" << std::endl; } else { std::cout << "String doesn't starts with prefix" << std::endl; } return 0; } |
Output:
String starts with the given prefix
That’s all about checking if a string starts with a certain string in C++.