string_algo库是一个非常全面的字符串算法库,提供了大量的字符串操作函数,如大小写无关比较,修剪,特定模式的子串查找等,可以再不使用正则表达式的情况下处理大多数字符串相关问题。弥补了std::string的一些短处。
#include <boost/algorithm/string.hpp> #include <iostream> #include <vector> void SimpleUse() { using namespace boost; using namespace std; string str("readme.txt"); if (ends_with(str, "txt")) { cout << to_upper_copy(str) + " UPPER" << endl; } replace_first(str, "readme", "followme"); cout << str << endl; vector<char> v(str.begin(), str.end()); vector<char> v2 = to_upper_copy( erase_first_copy(v, "txt")); for (int i = 0; i < v2.size();i++) { cout << v2[i]; } cout << endl; } void Upper_Lower() { using namespace boost; using namespace std; string str("I Don't Know!"); cout << to_upper_copy(str) << endl; cout << str << endl;; to_lower(str); cout << str << endl;; } void Predicates_use() { using namespace boost; std::string str("I Don't Know!"); assert(!starts_with(str, "i")); assert(istarts_with(str, "i")); assert(ends_with(str, "now!")); assert(icontains(str, "don't")); std::string str2 = to_lower_copy(str); assert(iequals(str, str2)); assert(!ilexicographical_compare(str, "a")); std::string str3 = "THIS is upper"; assert(all(str3.substr(0,4),is_upper())); } int main(int argc,char * argv[]) { SimpleUse(); Upper_Lower(); Predicates_use(); std::cin.get(); return 0; }
结果:
详细参考:http://www.boost.org/doc/libs/1_55_0/doc/html/string_algo/reference.html