boost::string_algo详解6——replace相关函数

来源:互联网 发布:编程ls是什么意思 编辑:程序博客网 时间:2024/06/18 07:01
replace的主要函数(以及其包括的copy函数)包括:
replace_range, replace_first, replace_last, replace_nth, replace_head, replace_tail, replace_regex, replace_all, replace_all_regex

[cpp] view plaincopyprint?
  1. void test_string_replace()  
  2. {  
  3.     using namespace boost;  
  4.     std::string str = "Hello Dolly, Hello World!";  
  5.   
  6.     std::cout << "#### replace_range ####" << std::endl;  
  7.     {  
  8.         std::string str1 = str;  
  9.         replace_range(str1, make_iterator_range(str1.begin() + 2, str1.end() - 2), "!!!");  
  10.         std::cout << str1 << std::endl;  
  11.     }  
  12.     std::cout << std::endl;  
  13.   
  14.     std::cout << "#### replace_first and replace_first_copy ####" << std::endl;  
  15.     {  
  16.         std::string str2 = str;  
  17.         replace_first(str2, "Hello""GoodBye");  
  18.         std::cout << str2 << std::endl;  
  19.     }  
  20.     {  
  21.         std::string str2 = str;  
  22.         std::string strc1 = replace_first_copy(str2, "Hello""GoodBye");  
  23.         std::cout << strc1 << std::endl;  
  24.   
  25.         std::string strc2 = "result = ";  
  26.         replace_first_copy(back_inserter(strc2), str, "Hello""GoodBye");  
  27.         std::cout << strc2 << std::endl;  
  28.     }  
  29.     std::cout << std::endl;  
  30.   
  31.     std::cout << "#### replace_last and replace_last_copy ####" << std::endl;  
  32.     {  
  33.         std::string str3 = str;  
  34.         replace_last(str3, "Hello""GoodBye");  
  35.         std::cout << str3 << std::endl;  
  36.     }  
  37.     {  
  38.         std::string str3 = str;  
  39.         std::string strc1 = replace_last_copy(str3, "Hello""GoodBye");  
  40.         std::cout << strc1 << std::endl;  
  41.   
  42.         std::string strc2 = "result = ";  
  43.         replace_last_copy(back_inserter(strc2), str, "Hello""GoodBye");  
  44.         std::cout << strc2 << std::endl;  
  45.     }  
  46.     std::cout << std::endl;  
  47.   
  48.     // 注意nth的索引从0开始.  
  49.     std::cout << "#### replace_nth and replace_nth_copy ####" << std::endl;  
  50.     {  
  51.         std::string str4 = str;  
  52.         replace_nth(str4, "Hello", 1, "GoodBye");  
  53.         std::cout << str4 << std::endl;  
  54.     }  
  55.     {  
  56.         std::string str4 = str;  
  57.         std::string strc1 = replace_nth_copy(str4, "Hello", 1, "GoodBye");  
  58.         std::cout << strc1 << std::endl;  
  59.   
  60.         std::string strc2 = "result = ";  
  61.         replace_nth_copy(back_inserter(strc2), str, "Hello", 1, "GoodBye");  
  62.         std::cout << strc2 << std::endl;  
  63.     }  
  64.     std::cout << std::endl;  
  65.   
  66.     std::cout << "#### replace_all and replace_all_copy ####" << std::endl;  
  67.     {  
  68.         std::string str5 = str;  
  69.         replace_all(str5, "Hello""GoodBye");  
  70.         std::cout << str5 << std::endl;  
  71.     }  
  72.     {  
  73.         std::string str5 = str;  
  74.         std::string strc1 = replace_all_copy(str5, "Hello""GoodBye");  
  75.         std::cout << strc1 << std::endl;  
  76.   
  77.         std::string strc2 = "result = ";  
  78.         replace_all_copy(back_inserter(strc2), str, "Hello""GoodBye");  
  79.         std::cout << strc2 << std::endl;  
  80.     }  
  81.     std::cout << std::endl;  
  82.   
  83.     std::cout << "#### replace_head and replace_head_copy ####" << std::endl;  
  84.     {  
  85.         std::string str6 = str;  
  86.         replace_head(str6, 5, "GoodBye");  
  87.         std::cout << str6 << std::endl;  
  88.     }  
  89.     {  
  90.         std::string str6 = str;  
  91.         std::string strc1 = replace_head_copy(str6, 5, "GoodBye");  
  92.         std::cout << strc1 << std::endl;  
  93.   
  94.         std::string strc2 = "result = ";  
  95.         replace_head_copy(back_inserter(strc2), str, 5, "GoodBye");  
  96.         std::cout << strc2 << std::endl;  
  97.     }  
  98.     std::cout << std::endl;  
  99.   
  100.     std::cout << "#### replace_tail and replace_tail_copy ####" << std::endl;  
  101.     {  
  102.         std::string str6 = str;  
  103.         replace_tail(str6, 6, "GoodBye");  
  104.         std::cout << str6 << std::endl;  
  105.     }  
  106.     {  
  107.         std::string str6 = str;  
  108.         std::string strc1 = replace_tail_copy(str6, 6, "GoodBye");  
  109.         std::cout << strc1 << std::endl;  
  110.   
  111.         std::string strc2 = "result = ";  
  112.         replace_tail_copy(back_inserter(strc2), str, 6, "GoodBye");  
  113.         std::cout << strc2 << std::endl;  
  114.     }  
  115.     std::cout << std::endl;  
  116.   
  117.     std::cout << "#### replace_regex, replace_regex_copy ####" << std::endl;  
  118.     {  
  119.         std::string str6 = str;  
  120.         regex reg("H.*?o");  
  121.   
  122. //      replace_regex(str6, reg, "GoodBye");    // 直接这么写会报错. 要用如下的方法.  
  123.         std::string strReplace = "GoodBye";  
  124.         replace_regex(str6, reg, strReplace);  
  125.   
  126.         std::cout << str6 << std::endl;  
  127.     }  
  128.   
  129.     {  
  130.         std::string str6 = str;  
  131.         regex reg("H.*?o");  
  132.         std::string strReplace = "GoodBye";  
  133.         std::string strc1 = replace_regex_copy(str6, reg, strReplace);  
  134.         std::cout << strc1 << std::endl;  
  135.   
  136.         std::string strc2 = "result = ";  
  137.         replace_regex_copy(back_inserter(strc2), str, reg, strReplace);  
  138.         std::cout << strc2 << std::endl;  
  139.     }  
  140.     std::cout << std::endl;  
  141.   
  142.     std::cout << "#### replace_all_regex, replace_all_regex_copy ####" << std::endl;  
  143.     {  
  144.         std::string str6 = str;  
  145.         regex reg("H.*?o");  
  146.         std::string strReplace = "GoodBye";  
  147.         replace_all_regex(str6, reg, strReplace);  
  148.         std::cout << str6 << std::endl;  
  149.     }  
  150.     {  
  151.         std::string str6 = str;  
  152.         regex reg("H.*?o");  
  153.         std::string strReplace = "GoodBye";  
  154.         std::string strc1 = replace_all_regex_copy(str6, reg, strReplace);  
  155.         std::cout << strc1 << std::endl;  
  156.   
  157.         std::string strc2 = "result = ";  
  158.         replace_all_regex_copy(back_inserter(strc2), str, reg, strReplace);  
  159.         std::cout << strc2 << std::endl;  
  160.     }  
  161.     std::cout << std::endl;  
  162. }