boost::algorithm用法详解之字符串关系判断

下面先列举几个常用的:

#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必须先包含下面头文件

#include <boost/algorithm/string.hpp>using namespace std;using namespace boost; 一、字母大小写转化

string str1(" hello world! "); to_upper(str1); // str1 == " HELLO WORLD! "string str1(" hello world! "); string str2; str2 = to_upper_copy(str1); // str2 == " HELLO WORLD! "Example:参看to_upper()Example:参看to_upper_copy()

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

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

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

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

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

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

四:查找1find_first()从头查找字符串中的子字符串,返回这个子串在原串中的iterator_range迭代器Example:char ToUpper(char &ch)char ToUpper(char &ch){if(ch <= ‘z’ && ch >= ‘a’)return ch + ‘A’-‘a’;elsereturn 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!"2ifind_first()从头查找字符串中的子字符串,返回这个子串在原串中的iterator_range迭代器(不区分大小写)

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

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

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

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

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

积极的人在每一次忧患中都看到一个机会,

boost::algorithm用法详解之字符串关系判断

相关文章:

你感兴趣的文章:

标签云: