1. 程式人生 > >boost::algorithm用法詳解之字符串關系判斷

boost::algorithm用法詳解之字符串關系判斷

ret 判斷 hello 第一個字符 pri 字符串替換 views .net private

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必須先包含下面頭文件

[cpp] view plain copy print?
  1. #include <boost/algorithm/string.hpp>
  2. using namespace std;
  3. using namespace boost;


一、字母大小寫轉化

1 to_upper() 將字符串轉為大寫

[cpp] view plain copy print?
  1. string str1(" hello world! ");
  2. to_upper(str1); // str1 == " HELLO WORLD! "


2 to_upper_copy() 將字符串轉為大寫,並且賦值給另一個字符串

[cpp] view plain copy print?
  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()

二:謂詞
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 <= ‘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!"
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!")

七:連接
// 我的Boost 1.33沒有實現?
1 join
2 join_if

boost::algorithm用法詳解之字符串關系判斷