字符串操作
使用boost库进行字符串的操作,主要通过引入<boost/algorithm/string.hpp>
,来提供操作支持。
字符串切割boost::algorithm::split()
#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
#include <iostream>
using namespace boost::algorithm;
int main()
{
std::string s = "Boost C++ Libraries";
std::vector<std::string> v;
split(v, s, is_space());
std::cout << v.size() << '\n';
}
对字符串进行大小写转换
boost::algorithm::to_upper_copy()
boost::algorithm::to_upper_copy()
vs
boost::algorithm::to_upper()
boost::algorithm::to_lower()
注:copy函数返回一个大小写变换之后的副本
移除特定字符
# 移除第一个出现的指定字符
erase_first_copy()
erase_nth_copy()
# 移除最后出现的指定字符
erase_last_copy()
# 移除所有出现的指定字符
erase_all_copy()
# 移除头部的指定字符
erase_head_copy()
# 移除尾部的指定字符
erase_tail_copy()
找到指定字符出现的位置
boost::algorithm::find_first()
boost::algorithm::find_last()
boost::algorithm::find_nth()
boost::algorithm::find_head()
boost::algorithm::find_tail()
字符串拼接 boost::algorithm::join()
int main()
{
std::vector<std::string> v{"Boost", "C++", "Libraries"};
std::cout << join(v, " ") << '\n';
}
字符替换
boost::algorithm::replace_first_copy()
boost::algorithm::replace_nth_copy()
boost::algorithm::replace_last_copy()
boost::algorithm::replace_all_copy()
boost::algorithm::replace_head_copy()
boost::algorithm::replace_tail_copy().