http://blog.csdn.net/qingzai_/article/details/44417937

下面先列举几个常用的:

#define i_end_with boost::iends_with
#define i_start_with boost::istarts_with
#define i_contain boost::icontains
#define i_equal boost::iequals
#define split boost::algorithm::split
#define i_replace boost::replace_all

要使用boost::algorithm必须先包含下面头文件

  1. #include <boost/algorithm/string.hpp>
  2. using namespace std;
  3. using namespace boost;

一、字母大小写转化

1 to_upper() 将字符串转为大写

  1. string str1(" hello world! ");
  2. to_upper(str1);  // str1 == " HELLO WORLD! "

2 to_upper_copy() 将字符串转为大写,并且赋值给另一个字符串

  1. string str1(" hello world! ");
  2. string str2;
  3. str2 = to_upper_copy(str1);  // str2 == " HELLO WORLD! "

3 to_lower() 将字符串转为小写
 Example:参看to_upper()
4 to_lower_copy() 将字符串转为小写,并且赋值给另一个字符串
 Example:参看to_upper_copy()

二:谓词
starts_with()  判断一个字符串是否是另外一个字符串的开始串
 Example:
 string str1("hello world!");
 string str2("hello");
 bool result = starts_with(str1, str2); // result == true
 
istarts_with() 判断一个字符串是否是另外一个字符串的开始串(不区分大小写)
 Example:
 string str1("hello world!");
 string str2("Hello");
 bool result = istarts_with(str1, str2); // result == true

ends_with()  判断一个字符串是否是另外一个字符串的结尾串
iends_with()  判断一个字符串是否是另外一个字符串的结尾串(不区分大小写)

contains() 判断一个字符串是否包含另外一个字符串
 Example:
 string str1("hello world!");
 string str2("llo");
 bool result = contains(str1, str2); // result == true
icontains() 判断一个字符串是否包含另外一个字符串(不区分大小写)

equals()  判断两个字符串是否相等
iequals()  判断两个字符串是否相等(不区分大小写)

lexicographical_compare()  按照字典排序,如果第一个字符串小于第二个字符串,返回true  (我的boost1.33没有实现?)
10 ilexicographical_compare() 按照字典排序,如果第一个字符串小于第二个字符串,返回true(不区分大小写)(我的boost1.33没有实现?

11 all() 判断字符串中的所有字符是否全部满足这个谓词
 Example:
 bool is_123digit(const char &ch)
 {
  if(ch == '1' || ch == '2' || ch == '3')
   return true;
  else
   return false;
 }
 ...
 string str1("12332211");
 bool result = all(str1, is_123digit); // result == true
 str1 = "412332211";
 result = all(str1, is_123digit); // result == false

四:查找
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!");

 五:删除/替换
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()

// 与上面类似,不过是从字符串n个匹配的开始替换
17 replace_nth() 
 Example:
 string str1("hello world!");
 replace_nth(str1, "o", 1, "O"); // str1 = "hello wOrld!"
18 replace_nth_copy()
19 ireplace_nth()
20 ireplace_nth_copy()
21 erase_nth()
22 erase_nth_copy()
23 ierase_nth()
24 ierase_nth_copy()

// 与上面类似,不过是将所有的匹配字符串替换
25 replace_all()
26 replace_all_copy()
27 ireplace_all()
28 ireplace_all_copy()
29 erase_all()
30 erase_all_copy()
31 ierase_all()
32 ierase_all_copy()

33 replace_head() 替换前n个字符
 Example:
 string str1("hello world!");
 replace_head(str1, 5, "HELLO"); // str1 = "HELLO world!"

34 replace_head_copy() 替换前n个字符,并且赋值给另一个字符串
 Example:
  string str1("hello world!");
 string str2;
 str2 = replace_head_copy(str1, 5, "HELLO"); // str2 = "HELLO world!"

35 erase_head() 删除前n个字符
 Example:
 string str1("hello world!");
 erase_head(str1, 5); // str1 = " world!"

36 erase_head_copy()  删除前n个字符,并且赋值给另一个字符串
 Example:
  string str1("hello world!");
 string str2;
 str2 = erase_head_copy(str1, 5); // str2 = " world!"

// 与上面类似(替换/删除后n个字符)
37 replace_tail() 
38 replace_tail_copy() 
39 erase_tail() 
40 erase_tail_copy()

// 与正则表示式相关,稍后了解。
41 replace_regex() 
42 replace_regex_copy() 
43 erase_regex() 
44 erase_regex_copy() 
45 replace_all_regex() 
46 replace_all_regex_copy() 
47 erase_all_regex() 
48 erase_all_regex_copy()

// 不是很清楚,稍后了解
49 find_format() 
50 find_format_copy() 
51 find_format_all() 
52 find_format_all_copy()

六:切割
find_all() 查找所有匹配的值,并且将这些值放到给定的容器中
 Example:
 string str1("hello world!");
 std::vector<std::string> result;
 find_all(result, str1, "l"); // result = [3]("l","l","l")

ifind_all() 查找所有匹配的值,并且将这些值放到给定的容器中(不区分大小写)

find_all_regex() 与正则表达式相关,稍后了解

split() 按照给定的谓词切割字符串,并且把切割后的值放入到给定的容器中
 Example:
 class SplitNotThisChar
 {
 public:
  SplitNotThisChar(const char ch) : m_char(ch) {}
  bool operator ()(const char &ch) const
  {
   if(ch == m_char)
    return true;
   else
    return false;
  }
 private:
  char m_char;
 };

string str1("hello world!");
 string str2;
 std::vector<std::string> result;
 split(result, str1, SplitNotThisChar('l'));  // result = [4]("he","","o wor","d!")

split_regex()  与正则表达式相关,稍后了解

iter_find() 按照给定Finder查找字符串,并且把查找到的值放入到给定的容器中
 Example:
 string str1("hello world!");
 std::vector<std::string> result;
 // first_finder为Boost自带的Finder
 iter_find(result, str1, first_finder("l")); // result = [3]("l","l","l")

iter_split() 按照给定的Finder切割字符串,并且把切割后的值放入到给定的容器中
 Example:
 string str1("hello world!");
 std::vector<std::string> result;
 iter_split(result, str1, first_finder("l")); // result = [4]("he","","o wor","d!")

七:连接
// 我的Boost 1.33没有实现?
join 
join_if

boost::algorithm用法详解之字符串关系判断的更多相关文章

  1. Boost::split用法详解

    工程中使用boost库:(设定vs2010环境)在Library files加上 D:\boost\boost_1_46_0\bin\vc10\lib在Include files加上 D:\boost ...

  2. [转] boost::function用法详解

    http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost/fun ...

  3. boost::function用法详解

    要开始使用 Boost.Function, 就要包含头文件 "boost/function.hpp", 或者某个带数字的版本,从 "boost/function/func ...

  4. boost::fucntion 用法详解

    转载自:http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost ...

  5. C#的String.Split 分割字符串用法详解的代码

    代码期间,把代码过程经常用的内容做个珍藏,下边代码是关于C#的String.Split 分割字符串用法详解的代码,应该对码农们有些用途. 1) public string[] Split(params ...

  6. PHP截取字符串函数substr()函数实例用法详解

    在PHP中有一项非常重要的技术,就是截取指定字符串中指定长度的字符.PHP对于字符串截取可以使用PHP预定义函数substr()函数来实现.下面就来介绍一下substr()函数的语法及其应用. sub ...

  7. Python 字符串、列表和元组用法详解

    1.通用函数 len() #列表的元素个数.字符串的长度 2.''' '''与'\ '用法详解 s='''this is a text ''' -->输出s ---> 'this\nis\ ...

  8. JavaScript中return的用法和this的用法详解

    JavaScript中return的用法详解 最近,跟身边学前端的朋友了解,有很多人对this和函数中的return的用法和意思理解的比较模糊,这里写一篇博客跟大家一起探讨一下return和this的 ...

  9. C++中的STL中map用法详解(转)

    原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解   Map是STL的一个关联容器,它提供 ...

随机推荐

  1. requests模块基础

    requests模块 .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { bor ...

  2. Python自动化运维——文件内容差异对比

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 模块:difflib 安装:Python版本大于等于2.3系统自带 功能:对比文本之间的差异,而且支持输出可读性比 ...

  3. poj_2339

    参考:https://blog.csdn.net/yzl_rex/article/details/7600906 https://blog.csdn.net/acm_JL/article/detail ...

  4. 联想ThinkPad S3-S440虚拟机安装,ubuntu安装,Hadoop(2.7.1)详解及WordCount运行,spark集群搭建

    下载ubuntu操作系统版本 ubuntu-14.10-desktop-amd64.iso(64位) 安装过程出现错误: This kernel requires an X86-64 CPU,but ...

  5. PRO*C 函数事例 2 -- 数据库操作

    Pro*C Oracle 的嵌入式开发,数据库处理部分最好能提取到一个模块,按照对不同数据库表的操作分成不同的.pc文件(如 DbsInstStat.pc).将此模块编译成库(c文件编译时链接此库), ...

  6. VHDL入门学习-程序组成

    1. VHDL程序的组成 一个完整的VHDL程序是以下五部分组成的: 2. 库(LIBRARY):比较好理解,调用系统已有的库,WORK库就是用户当前编辑文件所在的文件夹, IEEE库:由IEEE(美 ...

  7. Django笔记 —— 模型高级进阶

    最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...

  8. 「日常训练」 Soldier and Cards (CFR304D2C)

    题意 (Codeforces 546C) 按照指定的规则打牌,问谁胜或无穷尽. 分析 又是一条模拟,用set+queue(这里手写了)处理即可.注意到两种局势"1 234"和&qu ...

  9. Qt程序加图标

    第一步 准备一个ICON图标 例如:myicon.ico 新建文本文件,里面编辑文字 IDI_ICON1 ICON DISCARDABLE "myicon.ico" 文件另存为 x ...

  10. Python request 简单使用

    Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTT ...