下面总结了boost库常用字符串处理函数
头文件
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
代码:
#include<iostream>
#include<vector>
#include<boost/algorithm/string.hpp>
using namespace boost;
using namespace std;
bool is_123(const char &ch)
{
if (ch == '1' || ch == '2' || ch == '3')
return true;
else
return false;
}
char ToUpper(char &ch)
{
if (ch <= 'z' && ch >= 'a')
return ch + 'A' - 'a';
else
return ch;
}
char Add1(const char &ch)
{
return ch + 1;
}
void join_test()
{
vector<string> token;
token.push_back("jamin");
token.push_back("liu");
token.push_back("is");
token.push_back("a good boy!");
string temp_token = join(token, " ");
cout << "The temp_token is: " << temp_token << endl;
}
int main()
{
// 将字符串转为大写
string str1("jamin liu");
to_upper(str1);
cout << "The str1 is: " << str1 << endl;
// 将字符串转为大写,并赋值给另外一个字符串
str1 = "jaminliu";
string str2;
str2 = to_upper_copy(str1);
cout << "The str2 is: " << str2 << endl;
// 将字符串转为小写
str1 = "JAMIN LIU";
to_lower(str1);
cout << "The str1 is: " << str1 << endl;
// 将字符串转为小写,并赋值给另外一个字符串
str1 = "JAMIN LIU";
str2 = to_lower_copy(str1);
cout << "The str2 is: " << str2 << endl;
// 判断一个字符串是否是另外一个字符串的开始串
str1 = "jamin liu";
str2 = "jamin";
bool result = starts_with(str1, str2); // 判断str2是否是str1的开始串,有顺序之分
cout << "The result is: " << result << endl;
// 判断一个字符串是否是另外一个字符串的开始串(不区分大小写)
str1 = "jamin liu";
str2 = "JAMIN";
result = istarts_with(str1, str2); // 判断str1是否是str2的开始串,有顺序之分
cout << "The result is: " << result << endl;
// 判断一个字符串是否是另外一个字符串的结尾串
str1 = "jamin liu";
str2 = "liu";
result = ends_with(str1, str2); // 判断str2是否是str1的开始串,有顺序之分
cout << "The ends_with() result is: " << result << endl;
// 判断一个字符串是否是另外一个字符串的结尾串(不区分大小写)
str1 = "jamin liu";
str2 = "LIU";
result = iends_with(str1, str2); // 判断str1是否是str2的开始串,有顺序之分
cout << "The iends_with() result is: " << result << endl;
// 判断一个字符串是否包含另外一个字符串
str1 = "jaminliu";
str2 = "inl";
result = contains(str1, str2);
cout << "The contains() result is: " << result << endl;
// icontains() 判断一个字符串是否包含另外一个字符串(不区分大小写) --省略
// 判断两个字符串是否相等
str1 = "jaminliu";
str2 = "inl";
result = equals(str1, str2);
cout << "The equals() result is: " << result << endl;
// iequals() 判断两个字符串是否相等(不区分大小写) --省略
// 按照字典排序,如果第一个字符串小于第二个字符串,返回true
str1 = "jaminliu";
str2 = "inl";
result = lexicographical_compare(str2, str1);
cout << "The lexicographical_compare() result is: " << result << endl;
// ilexicographical_compare() 按照字典排序,如果第一个字符串小于第二个字符串,返回true(不区分大小写) --省略
// 判断字符串中的所有字符是否全部满足自定义的字符
str1 = "11233321";
result = all(str1, is_123);
cout << "The all() result is: " << result << endl;
// ☆☆☆ 查找 ☆☆☆
// 1 find_first() 从头查找字符串中的子字符串,返回这个子串在原串中的iterator_range迭代器
str1 = "hello wolld!";
iterator_range<string::iterator> result2 = find_first(str1, "ll");
transform(result2.begin(), result2.end(), result2.begin(), ToUpper);
cout << "The str1 find_first() is: " << str1 << endl; // str1 = "heLLo wolld!"
// 2 ifind_first() 从头查找字符串中的子字符串,返回这个子串在原串中的iterator_range迭代器(不区分大小写)
// 3 find_last() 从尾查找字符串中的子字符串,返回这个子串在原串中的iterator_range迭代器
str1 = "hello wolld!";
iterator_range<string::iterator> result3 = find_last(str1, "ll");
transform(result3.begin(), result3.end(), result3.begin(), ToUpper);
cout << "The str1 find_last() is: " << str1 << endl; // str1 = "hello woLLd!"
// 4 find_nth() 找到第n个匹配的子串(计算从0开始)
str1 = "hello wolld hello wolld !";
int temp = 0;
while (true)
{
iterator_range<string::iterator> result4 = find_nth(str1, "ll", temp);
if (result4.end() == str1.end())
break;
transform(result4.begin(), result4.end(), result4.begin(), ToUpper);
}
cout << "The str1 find_nth() is: " << str1 << endl; // str1 = "heLLo woLLd heLLo woLLd !"
// 5 find_head() 找到字符串的前n个字节
str1 = "hello wolld!";
iterator_range<string::iterator> result5 = find_head(str1, 5);
transform(result5.begin(), result5.end(), result5.begin(), ToUpper);
cout << "The str1 find_head() is: " << str1 << endl; // str1 = "HELLO woLLd!"
// 6 find_tail() 找到字符串的后n个字节
str1 = "hello wolld!";
iterator_range<string::iterator> result6 = find_tail(str1, 6);
transform(result6.begin(), result6.end(), result6.begin(), ToUpper);
cout << "The str1 find_tail() is: " << str1 << endl; // str1 = "hello WOLLD!"
// 7 find_token() 找到符合谓词的串
str1 = "hello 123321 world!";
iterator_range<string::iterator> result7 = find_token(str1, is_123);
transform(result7.begin(), result7.end(), result7.begin(), Add1); // str1 == "hello 2 world!");
cout << "The str1 find_token() is: " << str1 << endl; // str1 = "hello 223321 world!"
// ☆☆☆ 删除/替换 ☆☆☆
// 1 replace_first() 从头找到第一个匹配的字符串,将其替换为给定的另外一个字符串
str1 = "hello wolld hello!";
replace_first(str1, "hello", "HELLO");
cout << "The str1 replace_first() is: " << str1 << endl; // str1 = "HELLO wolld hello!"
// 2 replace_first_copy() 从头找到第一个匹配的字符串,将其替换为给定的另外一个字符串,并且赋值给另一个字符串
// str2 = replace_first_copy(str1, "hello", "Hello"); // str2 = "Hello world!"
// 3 ireplace_first() 从头找到第一个匹配的字符串,将其替换为给定的另外一个字符串(不区分大小写
// 4 erase_first() 从头找到第一个匹配的字符串,将其删除
str1 = "hello wolld hello!";
erase_first(str1, "hello");
cout << "The str1 erase_first() is: " << str1 << endl; // str1 = " wolld hello!"
// 5 erase_first_copy() 从头找到第一个匹配的字符串,将其删除,并且赋值给另一个字符串
// 6 ierase_first() 从头找到第一个匹配的字符串,将其删除(不区分大小写)
// 7 replace_all() 从头找到所有匹配的字符串,将其替换为给定的另外一个字符串
str1 = "hello wolld hello!";
replace_all(str1, "hello", "HELLO");
cout << "The str1 replace_all() is: " << str1 << endl; // str1 = "HELLO wolld HELLO!"
// ☆☆☆ 切割 ☆☆☆
// 1 find_all() 查找所有匹配的值,并且将这些值放到给定的容器中
str1 = "hello wolld hello!";
vector<string> result_string;
find_all(result_string, str1, "l");
cout << "The result_string size is: " << result_string.size() << endl; // size = 6
// 2 split() 按照给定的谓词切割字符串,并且把切割后的值放入到给定的容器中
str1 = "hello wolld hello!";
vector<string> result_string2;
split(result_string2, str1, boost::is_any_of(" "));
for (auto t : result_string2)
{
cout << t << "||";
} // hello||wolld||hello!||
cout << endl;
// 3 iter_split() 按照给定的Finder切割字符串,并且把切割后的值放入到给定的容器中
str1 = "hello wolld hello!";
vector<string> result_string3;
iter_split(result_string3, str1, first_finder("l"));
for (auto t : result_string3)
{
cout << t << " ";
} // he o wo d he o!
cout << endl;
// ☆☆☆ 连接 ☆☆☆
// 1 join:将字符串 列表,使用指定的字符,连接起来,返新的字符串。
join_test();
system("pause");
return 0;
}
结果:

参考链接:
https://blog.youkuaiyun.com/zhangyuanxuevaq/article/details/79467650
本文介绍Boost库中的字符串处理函数,包括大小写转换、查找、替换、切割和连接等功能,并通过示例代码展示如何使用这些函数。
435

被折叠的 条评论
为什么被折叠?



