zoukankan      html  css  js  c++  java
  • boost::algorithm(字符串算法库)

    没什么说的,需要

    #include<boost/algorithm/string.hpp>

    1.大小写转换

        std::string s("test string");
        
        boost::to_upper(s);//转换为大写
        boost::to_lower(s);//转换为小写
    
        std::string str1=boost::to_lower_copy(s);//小写转换并赋值
        std::string str2=boost::to_upper_copy(s);//大写转换并赋值
        std::array<string, 3> k = {"hello", "world", "123"};
        std::cout << join(k, "-");        //输出结果为: hello-world-123


    2.分割与合并字符串

        std::string s("test stringa-test stringb-test stringc");
        std::vector<std::string> sv;
        boost::split(sv,s,boost::is_any_of("-"),boost::token_compress_on);
        //Now,sv={"test stringa","test stringb","test stringc"};

    3.去掉字符串两边空格

        std::string s("      test string      ");
        boost::trim_left(s);//去掉字符串左边空格
        boost::trim_right(s);//去掉字符串右边空格
        //现在s="test string"
        //boost::trim_left_copy(s)和boost::trim_right_copy(s)表示去掉后赋值
      

     trim_left_copy_if() 将字符串开头的符合我们提供的“谓词”的特定字符去掉,并且赋值给另一个字符串
     string str1(" hello world! ");
     string str2;
     str2 = trim_left_copy_if(str1, NotH);      // str2 == "ello world! "

    总结一下就是凡是有copy就是指向后赋值,有if就判断谓词

    3.谓词

        std::string s("test string");
        boost::starts_with("test");//判断字符串是否以一个字符串开始,返回bool

        std::string a("sss");
        std::string b("sss");
        boost::equal(a,b);//判断字符串是否完全匹配

        std::string s("test string");
        boost::contains("te");//判断字符串是否含有某字符串
        boost::ends_with("ing");//判断字符串是否以另一个字符串结尾;
         // boost::iends_with()同上只是不区分大小写
    all()判断字符串中的所有字符是否全部满足这个谓词
        std::string s("test string");
    
        bool str_equal(const std::string p){
            if(boost::equal(p,"test string"))
                return true;
            return false;
        }
        boost::all(s,str_equal);

    4.查找字符串

    这里复制粘贴一段

    find_first() 从头查找字符串中的子字符串,返回这个子串在原串中的iterator_range迭代器
     Example:
     char ToUpper(char &ch)
     char ToUpper(char &ch)
     {
      if(ch <= 'z' && ch >= 'a')
       return ch + 'A'-'a';
      else
       return ch;
     }
     ...
     string str1("hello dolly!");
     iterator_range<string::iterator> result = find_first(str1,"ll");
     transform( result.begin(), result.end(), result.begin(), ToUpper ); // str1 == "heLLo dolly!"
    ifind_first() 从头查找字符串中的子字符串,返回这个子串在原串中的iterator_range迭代器(不区分大小写)

    find_last() 从尾查找字符串中的子字符串,返回这个子串在原串中的iterator_range迭代器
    ifind_last() 从尾查找字符串中的子字符串,返回这个子串在原串中的iterator_range迭代器(不区分大小写)

    find_nth() 找到第n个匹配的子串(计算从0开始)
     Example:
     string str1("hello dolly!");
     iterator_range<string::iterator> result = find_nth(str1,"ll", 1);
     transform( result.begin(), result.end(), result.begin(), ToUpper ); // str1 == "hello doLLy!"
    ifind_nth() 找到第n个匹配的子串(计算从0开始)(不区分大小写)

    find_head() 找到字符串的前n个字节
     Example:
     string str1("hello dolly!");
     iterator_range<string::iterator> result = find_head(str1,5);
     transform( result.begin(), result.end(), result.begin(), ToUpper ); // str1 == "HELLO dolly!"
    find_tail() 找到字符串的后n个字节

    find_token() 找到符合谓词的串
     Example:
     char Add1(const char &ch)
     {
      return ch+1;
     }
     ...
     string str1("hello 1 world!");
     iterator_range<string::iterator> result = find_token(str1,is_123digit);
     transform( result.begin(), result.end(), result.begin(), Add1 ); // str1 == "hello 2 world!");

    10 find_regex() 匹配正则表达式
     Example:(等稍候了解了boost的正则表达式后再给出)

    11 find() 使用自己写的查找函数
     Example:
     iterator_range<string::iterator>
     MyFinder1( std::string::iterator begin, std::string::iterator end )
     {
      std::string::iterator itr;
      for(itr = begin;itr!=end;itr++)
      {
       if((*itr) == '1')
       {
        std::string::iterator preitr = itr;
        iterator_range<string::iterator> ret(preitr, ++itr);
        return ret;
       }
      }
      return iterator_range<string::iterator>();
     } // boost自己也提供了很多Finder
     ...
     string str1("hello 1 world!");
     iterator_range<string::iterator> result = find(str1,MyFinder1);
     transform( result.begin(), result.end(), result.begin(), Add1 ); // str1 == "hello 2 world!");

    5.替换/删除字符串

    replace_first() 从头找到第一个匹配的字符串,将其替换为给定的另外一个字符串
     Example:
     string str1("hello world!");
     replace_first(str1, "hello", "Hello"); // str1 = "Hello world!"
    replace_first_copy()  从头找到第一个匹配的字符串,将其替换为给定的另外一个字符串,并且赋

    值给另一个字符串
     Example:
     string str1("hello world!");
     string str2;
     str2 = replace_first_copy(str1, "hello", "Hello"); // str2 = "Hello world!"
    ireplace_first()  从头找到第一个匹配的字符串,将其替换为给定的另外一个字符串(不区分大小写

    )
    ireplace_first_copy()  从头找到第一个匹配的字符串,将其替换为给定的另外一个字符串,并且赋

    值给另一个字符串(不区分大小写)
    erase_first()   从头找到第一个匹配的字符串,将其删除
     Example:
     string str1("hello world!");
     erase_first(str1, "llo"); // str1 = "He world!"
    erase_first_copy() 从头找到第一个匹配的字符串,将其删除,并且赋值给另一个字符串
     Example:
     string str1("hello world!");
     string str2;
     str2 = erase_first_copy(str1, "llo"); // str2 = "He world!"
    ierase_first()  从头找到第一个匹配的字符串,将其删除(不区分大小写)
    8 ierase_first_copy() 从头找到第一个匹配的字符串,将其删除,并且赋值给另一个字符串(不区分大

    小写)

    // 与上面类似,不过是从字符串尾开始替换
    9 replace_last()
    10 replace_last_copy()
    11 ireplace_last()
    12 ireplace_last_copy()
    13 erase_last()
    14 erase_last_copy()
    15 ierase_last()
    16 ierase_last_copy()



  • 相关阅读:
    GridView合并表头多重表头
    C# 导出Excel或Word
    GridView的分页功能?
    如何在GridView中判断Radio被选中?
    GridView無數據時,顯示表頭
    Oracle replace函数使用
    获取数据后导出Excel
    Oracel用rownum实现真分页
    转载C#泛型集合—Dictionary<K,V>使用技巧
    临时向表插入有自增的字段的记录
  • 原文地址:https://www.cnblogs.com/ysherlock/p/7822301.html
Copyright © 2011-2022 走看看