---
title: "boost::string(转)"
author: "lunan"
date: "2012-06-19T00:55:37Z"
tags: []
categories: ["编程语言"]
slug: "boost-string"
draft: false
lastmod: "2012-06-19T00:55:37Z"
id: 1563575
url: "https://lunan.iteye.com/blog/1563575"
---

boost::algorithm提供了很多字符串算法，包括： 大小写转换； 去除无效字符； 谓词； 查找； 删除/替换； 切割； 连接； 我们用写例子的方式来了解boost::algorithm能够为我们做些什么。

boost::algorithm学习  
#include &lt;boost/algorithm/string.hpp&gt;  
using namespace std;  
using namespace boost;   
**一：大小写转换**  
1 **to\_upper()** 将字符串转为大写  
Example:  
string str1(" hello world! ");  
to\_upper(str1); // str1 == " HELLO WORLD! "

2 **to\_upper\_copy()** 将字符串转为大写，并且赋值给另一个字符串  
Example:  
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()** 将字符串开头的空格去掉  
Example:  
string str1(" hello world! ");  
trim\_left(str1);      // str1 == "hello world! "

2 **trim\_left\_if()** 将字符串开头的符合我们提供的“谓词”的特定字符去掉  
Example:  
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()** 将字符串开头的空格去掉，并且赋值给另一个字符串  
Example:  
string str1(" hello world! ");  
string str2;  
str2 = trim\_left\_copy(str1); // str2 == "hello world! "

4 **trim\_left\_copy\_if()** 将字符串开头的符合我们提供的“谓词”的特定字符去掉，并且赋值给另一个字符串  
Example:  
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()** 判断一个字符串是否是另外一个字符串的开始串  
Example:  
string str1("hello world!");  
string str2("hello");  
bool result = starts\_with(str1, str2); // result == true

2 **istarts\_with()** 判断一个字符串是否是另外一个字符串的开始串(不区分大小写)  
Example:  
string str1("hello world!");  
string str2("Hello");  
bool result = istarts\_with(str1, str2); // result == true

3 **ends\_with()** 判断一个字符串是否是另外一个字符串的结尾串  
4 **iends\_with()** 判断一个字符串是否是另外一个字符串的结尾串(不区分大小写)

5 **contains()** 判断一个字符串是否包含另外一个字符串  
Example:  
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()** 判断字符串中的所有字符是否全部满足这个谓词  
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

**四：查找**  
1 **find\_first()** 从头查找字符串中的子字符串，返回这个子串在原串中的iterator\_range迭代器  
Example:  
char ToUpper(char &ch)  
char ToUpper(char &ch)  
{  
   if(ch &lt;= 'z' && ch &gt;= 'a')  
    return ch + 'A'-'a';  
   else  
    return ch;  
}  
...  
string str1("hello dolly!");  
iterator\_range&lt;string::iterator&gt; 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&lt;string::iterator&gt; 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&lt;string::iterator&gt; 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&lt;string::iterator&gt; 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&lt;string::iterator&gt;  
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&lt;string::iterator&gt; ret(preitr, ++itr);  
     return ret;  
    }  
   }  
   return iterator\_range&lt;string::iterator&gt;();  
} // boost自己也提供了很多Finder  
...  
string str1("hello 1 world!");  
iterator\_range&lt;string::iterator&gt; 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!"  
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&lt;std::string&gt; 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&lt;std::string&gt; 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&lt;std::string&gt; 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&lt;std::string&gt; result;  
iter\_split(result, str1, first\_finder("l")); // result = \[4]("he","","o wor","d!")