头文件:

#include<iostream>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;

函数及使用:

大小写转换

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

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

2.  to_upper_copy()  将字符串大写并返回大写字符串

    string str1(" hello world! ");
     string str2;
     str2 = to_upper_copy(str1);  // str2 == " HELLO WORLD! "

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

Trimming(整理,去掉首尾的空格字符)

1.   trim_left()   将字符串开头的空格去掉
       string str1(" hello world! ");
       trim_left(str1);      // str1 == "hello world! "
 
2    trim_left_if() 将字符串开头的符合我们提供的“谓词”的特定字符去掉
       bool NotH(const char &ch)
       {
          if(ch == ' ' || ch == 'H' || ch == 'h')
             return true;
          else
             return false;
       }
       ....
       string str1(" hello world! ");
       trim_left_if(str1, NotH);      // str1 == "ello world! "
 
3     trim_left_copy() 将字符串开头的空格去掉,并且赋值给另一个字符串
         string str1(" hello world! ");
         string str2;
         str2 = trim_left_copy(str1); // str2 == "hello world! "
 
4     trim_left_copy_if() 将字符串开头的符合我们提供的“谓词”的特定字符去掉,并且赋值给另一个字符串
         string str1(" hello world! ");
         string str2;
         str2 = trim_left_copy_if(str1, NotH);      // str2 == "ello world! "
 
        // 将字符串结尾的空格去掉,示例请参看上面
5     trim_right_copy_if()
6     trim_right_if()
7     trim_right_copy()
8     trim_right()

     // 将字符串开头以及结尾的空格去掉,示例请参看上面
9     trim_copy_if()
10    trim_if()
11    trim_copy()
12      trim()

谓词
1 starts_with()  判断一个字符串是否是另外一个字符串的开始串

     string str1("hello world!");
     string str2("hello");
     bool result = starts_with(str1, str2); // result == true
 
2 istarts_with() 判断一个字符串是否是另外一个字符串的开始串(不区分大小写情况下)

     string str1("hello world!");
     string str2("Hello");
     bool result = istarts_with(str1, str2); // result == true

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

5 contains()     判断一个字符串是否包含另外一个字符串

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

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

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

11 all() 判断字符串中的所有字符是否全部满足这个谓词
       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

查找
1    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!"
2 ifind_first() 从头查找字符串中的子字符串,返回这个子串在原串中的iterator_range迭代器(不区分大小写)

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

5 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!"
6 ifind_nth() 找到第n个匹配的子串(计算从0开始)(不区分大小写)

7 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!"
8 find_tail() 找到字符串的后n个字节

9 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!");

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

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

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

值给另一个字符串(不区分大小写)
5 erase_first()   从头找到第一个匹配的字符串,将其删除
 Example:
 string str1("hello world!");
 erase_first(str1, "llo"); // str1 = "He world!"
6 erase_first_copy() 从头找到第一个匹配的字符串,将其删除,并且赋值给另一个字符串
 Example:
 string str1("hello world!");
 string str2;
 str2 = erase_first_copy(str1, "llo"); // str2 = "He world!"
7 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()

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

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

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

4 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!")

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

6 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")

7 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!")

Join

Join函数则是和split相反,用于把多个字符串拼接起来。

std::array<string, 3> k = {"hello", "world", "123"};
    cout << join(k, "-");        //输出结果为: hello-world-123

boost之algorithm/string的更多相关文章

  1. 第116讲 boost::algorithm::string之替换和删除

    http://www.360doc.com/content/16/0523/18/29304643_561672752.shtml

  2. UESTC_韩爷的梦 2015 UESTC Training for Search Algorithm & String<Problem N>

    N - 韩爷的梦 Time Limit: 200/100MS (Java/Others)     Memory Limit: 1300/1300KB (Java/Others) Submit Stat ...

  3. UESTC_Palindromic String 2015 UESTC Training for Search Algorithm & String<Problem M>

    M - Palindromic String Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 128000/128000KB (Java ...

  4. UESTC_Ferris Wheel String 2015 UESTC Training for Search Algorithm & String<Problem L>

    L - Ferris Wheel String Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 43000/43000KB (Java/ ...

  5. UESTC_秋实大哥の恋爱物语 2015 UESTC Training for Search Algorithm & String<Problem K>

    K - 秋实大哥の恋爱物语 Time Limit: 5000/2000MS (Java/Others)     Memory Limit: 32000/32000KB (Java/Others) Su ...

  6. UESTC_Eight Puzzle 2015 UESTC Training for Search Algorithm & String<Problem F>

    F - Eight Puzzle Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) ...

  7. UESTC_吴队长征婚 2015 UESTC Training for Search Algorithm & String<Problem E>

    E - 吴队长征婚 Time Limit: 10000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  8. UESTC_基爷的中位数 2015 UESTC Training for Search Algorithm & String<Problem D>

    D - 基爷的中位数 Time Limit: 5000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  9. UESTC_基爷与加法等式 2015 UESTC Training for Search Algorithm & String<Problem C>

    C - 基爷与加法等式 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Subm ...

随机推荐

  1. C#.net 创建XML

    方法一:按照XML的结构一步一步的构建XML文档.    通过.Net FrameWork SDK中的命名空间"System.Xml"中封装的各种类来实现的 方法二:直接定影XML ...

  2. Navicat远程连接MySQL数据库

    1.打开Navicat,在界面的“主机名和IP地址”处输入IP地址,一般是192.168.1.1 2.输入相应的用户名和密码,点击连接测试,确认是否已经连接,之后就可以点击确定了 3.找到相应的数据库 ...

  3. Java如何等待子线程执行结束

    工作中往往会遇到异步去执行某段逻辑, 然后先处理其他事情, 处理完后再把那段逻辑的处理结果进行汇总的产景, 这时候就需要使用线程了. 一个线程启动之后, 是异步的去执行需要执行的内容的, 不会影响主线 ...

  4. ios 利用Reveal来调试界面2--真机调试(步骤详解)

    使用真机调试我们的App界面,如果你的真机是没有越狱的设备,那么使用Reveal来调试UI的步骤是最麻烦的.

  5. 对tomcat来说,每一个进来的请求(request)都需要一个线程,直到该请求结束。

    这段时间折腾了哈java web应用的压力测试,部署容器是tomcat 7.期间学到了蛮多散碎的知识点,及时梳理总结,构建良好且易理解的知识架构把它们组织起来,以备忘.对web应用开发者来说,我们很关 ...

  6. EasyUI-动态添加tab

    layout效果 代码: <%@ page language="java" contentType="text/html; charset=utf-8" ...

  7. 在Excel中创建和使用ServerDocument

    ServerDocument是微软提供的一种读取Word或Excel文档级应用中CachedData的工具.本示例将向你展示如何使用用ServerDocument. 1.      创建文档级应用 打 ...

  8. hadoop错误java.io.IOException Failed to replace a bad datanode on the existing pipeline due to no more good datanodes being available to try

    错误: java.io.IOException: Failed to replace a bad datanode on the existing pipeline due to no more go ...

  9. CPP: 跨平台生成GUID/UUID

    #ifndef XGUID_H#define XGUID_H #include <string>#include <stdio.h>#ifdef WIN32#include & ...

  10. Android(java)学习笔记176:BroadcastReceiver之 短信发送的广播接收者

    有时候,我们需要开发出来一个短信监听器,监听用户发送的短信记录,下面就是一个案例,这里同样需要使用广播机制. 下面同样是代码示例,MainActivity.java 和  activity_main. ...