boost字符串算法.doc
文本预览下载声明
boost的字符串算法 收藏
boost::algorithm简介
boost::algorithm提供了很多字符串算法,包括: 大小写转换; 去除无效字符; 谓词; 查找; 删除/替换; 切割; 连接; 我们用写例子的方式来了解boost::algorithm能够为我们做些什么。
boost::algorithm学习
#include boost/algorithm/string.hpp
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 = con
显示全部