Find out if string ends with another string in C++
问题 How can I find out if a string ends with another string in C++? 回答1: Simply compare the last n characters using std::string::compare: #include <iostream> bool hasEnding (std::string const &fullString, std::string const &ending) { if (fullString.length() >= ending.length()) { return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending)); } else { return false; } } int main () { std::string test1 = "binary"; std::string test2 = "unary"; std::string test3 =