boost——字符串与文本处理string

    xiaoxiao2021-12-15  41

    #include <iostream> #include <string> #include <vector> #include <set> #include <map> #include <algorithm> #include <boost/algorithm/string.hpp> #include <boost/format.hpp> using namespace boost; using namespace std; int main( int argc,char **argv) { string str("readme.txt"); if(ends_with(str,"txt")){ cout << to_upper_copy(str) + "UPPER" << endl; assert(ends_with(str,"txt")); } 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; /*大小写转换*/ string str1("I Don't Known.\n"); cout << to_upper_copy(str1) << endl; cout << str1 << endl; to_lower(str1); cout << str1 << endl; /*判断式算法*/ string str2("Power Bob"); assert(iends_with(str2,"bob")); //忽略大小写,后缀比较 assert(!ends_with(str2,"bob")); //不忽略大小写,后缀比较 assert(starts_with(str2,"Pow")); //检测前缀 assert(contains(str2,"er")); //检测包含关系 string str3 = to_lower_copy(str2); //转换小写 assert(iequals(str3,str2)); //大小写无关判断相等 string str4("power suit"); assert(ilexicographical_compare(str2,str4)); //大小无关比较 assert(all(str3.substr(0,5),is_lower())); //检测大小写均小写 /*判断式函数对象*/ string str5("Anna"); string str6("anna"); assert(!is_equal()(str5,str6)); assert(is_less()(str5,str6)); /*修剪与分类*/ format fmt("|%s|\n"); string str7 = " samus aran "; cout << fmt % trim_copy(str7) << endl; //删除两端的空格 cout << fmt % trim_left_copy(str7) << endl; //删除左端空格 trim_right(str7); //原地删除右端的空格 cout << fmt % str7; string str8 = "2016 hello world!"; cout << str8 << endl; cout << fmt % trim_left_copy_if(str8,is_digit()); cout << fmt % trim_right_copy_if(str8,is_punct()); cout << fmt % trim_copy_if(str8,is_punct() || is_digit() || is_space());//删除两端的标点、数字和空格 /*查找*/ string str9 = "Long long ago,there was a king"; format fmt2("|%s|.pos = %d\n"); iterator_range<string::iterator> rge; //迭代器区间 rge = find_first(str9,"long"); //查找第一次出现 cout << fmt2 % rge %(rge.begin()-str9.begin()); rge = ifind_first(str9,"long"); //与大小写无关的第一次出现的位置 cout << fmt2 % rge %(rge.begin()-str9.begin()); rge = find_nth(str9,"ng",2); //第三次出现的位置 cout << fmt2 % rge %(rge.begin()-str9.begin()); rge = find_head(str9,4); //查找前四个字符 cout << fmt2 % rge %(rge.begin()-str9.begin()); rge = find_tail(str9,5); //查找后五个字符 cout << fmt2 % rge %(rge.begin()-str9.begin()); rge = find_first(str9,"ewy89"); //若没找到 assert(rge.empty() && !rge); /*替换与删除*/ string str10 = "Long long ago,there was a king\n"; cout << replace_first_copy(str10,"long","Long "); replace_last(str10,"ng","mm"); cout << str10 << endl; replace_tail(str10,4,"wwww"); cout << str10 << endl; cout << ierase_all_copy(str10,"ago"); cout << replace_nth_copy(str10,"e",1,"1"); cout << erase_tail_copy(str10,10); return (0); }
    转载请注明原文地址: https://ju.6miu.com/read-1000132.html

    最新回复(0)