Check if a string ends with another string in C++
This post will discuss how to check if a string ends with another string in C++.
1. Using string::compare
The string::compare function compares the value of a string with the specified string and returns a zero status code if both strings are equal. The idea is to compare the last n characters with the given string with string::compare. It can be used as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> bool endsWith(std::string const &str, std::string const &suffix) { if (str.length() < suffix.length()) { return false; } return str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0; } int main() { std::string str = "C++20"; std::string suffix = "20"; std::cout << std::boolalpha << endsWith(str, suffix) << std::endl; // true return 0; } |
2. Using std::equal
Alternatively, we can use the std::equal standard algorithm to determine if elements in the two ranges are equal. We can use it as follows to compare the last n characters of a string with another string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> bool endsWith(std::string const &str, std::string const &suffix) { if (str.length() < suffix.length()) { return false; } return std::equal(suffix.rbegin(), suffix.rend(), str.rbegin()); } int main() { std::string str = "C++20"; std::string suffix = "20"; std::cout << std::boolalpha << endsWith(str, suffix) << std::endl; // true return 0; } |
3. Using Boost
If you’re already using boost library, you can use the boost::algorithm::ends_with function from the header <boost/algorithm/string/predicate.hpp>. The following code example shows invocation for this function:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <string> #include <boost/algorithm/string/predicate.hpp> int main() { std::string str = "C++20"; std::string suffix = "20"; std::cout << std::boolalpha << boost::algorithm::ends_with(str, suffix) << std::endl; return 0; } |
4. Using std::mismatch
The standard 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. We can check if the string ends with a given string using it by iterating backward from the end of both strings.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> #include <string> #include <algorithm> int main() { std::string str = "C++20"; std::string suffix = "20"; bool b = std::mismatch(suffix.rbegin(), suffix.rend(), str.rbegin()).first == suffix.rend(); std::cout << std::boolalpha << b << std::endl; // true return 0; } |
5. 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. It can be used as follows to check if the string ends with the specified string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> #include <algorithm> bool endsWith(std::string const &str, std::string const &suffix) { if (str.length() < suffix.length()) { return false; } return str.rfind(suffix) == str.size() - suffix.size(); } int main() { std::string str = "C++20"; std::string suffix = "20"; std::cout << std::boolalpha << endsWith(str, suffix) << std::endl; // true return 0; } |
6. Using string::ends_with
Starting with C++20, std::string finally provides the string::ends_with function, which returns true if the string ends with the specified suffix. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <string> int main() { std::string str = "C++17"; std::string suffix = "20"; std::cout << std::boolalpha << str.ends_with(suffix) << std::endl; // true return 0; } |
That’s all about checking if a string ends with another string in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)