The Boost.StringAlgorithms library provides many free-standing functions for string manipulation.

1. converting strings to uppercase

#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream> using namespace boost::algorithm int main() {
std::string s = "Boost C++ Libraries";
std::cout << to_upper_copy(s) << std::endl;
return ;
}

The function boost::algorithm::to_upper_copy() converts a string to uppercase, and boost::algorithm::to_lower_copy() converts a string to lowecase. Both functions return a copy of the input string, converted to the specified case. To convert the string in place, use the functions boost::algorithm::to_upper() or boost::algorithm::to_lower().

2. Algorithms to remove characters from a string

#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream> using namespace boost::algorithm int main() {
std::string s = "Boost C++ Libraries";
std::cout << erase_first_copy(s, "s") << std::endl; 删除第一个出现的s字符
std::cout << erase_nth_copy(s, "s", ) << std::endl;
std::cout << erase_last_copy(s, "s") << std::endl;
std::cout << erase_all_copy(s, "s") << std::endl;
std::cout << erase_head_copy(s, ) << std::endl; 删除从头部开始后的5个字符
std::cout << erase_tail_copy(s, ) << std::endl; 删除从尾部开始的9个字符
return ;
}

输出结果为:

Boot C++ Libraries

Boot C++ Libraries

Boost C++ Librarie

Boot C++ Librarie

C++ Libraries

Boost C++

3. Searching for substrings

#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream> using namespace boost::algorithm; int main() {
std::string s = "Boost C++ Libraries";
boost::iterator_range<std::string::iterator> r = find_first(s, "C++");
std::cout << r << std::endl;
r = find_first(s, "xyz");
std::cout << r << std::endl;
return ;
}

输出为:

C++

functions such as boost::algorithm::find_first(), boost::algorithm::find_last(), boost::algorithm::find_nth(), boost::algorithm::find_heand() and boost::algorithm::find_tail(), these functions return a pair of iterators of type boost::iterator_range. Because the operator operator<< is overloaded for boost::iterator_range, the result of the individual search algorithm can be written directly to standard output.

4. Concatenatin strings with boost::algorithm::join() 连接字符串

#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
#include <iostream> using namespace boost::algorithm; int main() {
std::vector<std::string> v{"Boost", "C++", "Libraries"};
join(v, " ");
return ;
}

输出为:Boost C++ Libraries

5. replace characters in a string

functions: 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()

6. trim strings

functions: boost::algorithm::trim_left_copy(), boost::algorithm::trim_right_copy(), boost::algorithm::trim_copy() to remove spaces on either end of a string.

Boost.StringAlgorithms lets you provide a predicate as an additional parameter for different functions to determine which characters of the string the function is applied to. The versions with predicate are: boost::algorithm::trim_right_copy_if(), boost::algorithm::trim_left_copy_if(), boost::algorithm::trim_copy_if()

7. creating predicates with boost::algorithm::is_any_of()

#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream> using namespace boost::algorithm; int main() {
std::string s = "--Boost C++ Libraries--";
std::cout << trim_left_copy_if(s, is_any_of("-")) << std::endl;
std::cout << trim_right_copy_if(s, is_any_of("-")) << std::endl;
std::cout << trim_copy_if(s, is_any_of("-")) << std::endl;
return ;
}

输出为:

Boost C++ Libraries--

--Boost C++ Libraries

Boost C++ Libraries

function called boost::algorithm::is_any_of(), which is a helper function to create a predicate that checks whether a certain character--passed as parameter to is_any_of() exists in a string. With boost::algorithm::is_any_of(), the characters for trimming a string can be specified.

many helper functions that return commonly used predicates: boost::algorithm::is_digit(), boost::algorithm::is_upper(), boost::algorithm::is_lower().

8. Algorithms to compare strings with others

#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream> using namespace boost::algorithm; int main() {
std::string s = "Boost C++ Libraries";
std::cout.setf(std::ios::boolalpha);
std::cout < starts_with(s, "Boost") << std::endl;
std::cout << ends_with(s, "Libraries") << std::endl;
std::cout << contains(s, "C++") << std::endl;
std::cout << lexicographical_compare(s, "Boost") << std::endl;
return ;
}

输出为:

true

true

true

false

9. Splitting strings with 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() << std::endl;
return ;
}

a given string can be split based on a delimiter. The substrings are stored in a container. The function requires as its third parameter a predicate that tests each character an checks whether the string should be split at the given position.

10. Searching strings with boost::algorithm::find_regex()

#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <string>
#include <iostream> using namespace boost::algorithm; int main() {
std::string s = "Boost C++ Libraries";
boost::iterator_range<std::string::iterator> r = find_regex(s, boost::regex{"\\w\\+\\+"});
std::cout << r << std::endl;
return ;
}

输出为:C++

最新文章

  1. ubuntu中用户使用的shell如何指定
  2. R语言进阶
  3. 关于ps中的锯齿
  4. [bzoj1854][SCOI2010]游戏
  5. [solr] - IKAnalyzer 分词加入
  6. jQuery easyui combobox级联及内容联想
  7. redmine慢---提速方法
  8. 如何把in_array 的第三个参数strict设置为 true
  9. debug实战:进程Hang+High CPU
  10. 使用MyEclipse可视化开发Hibernate实例
  11. Css静态进度条
  12. centos下安装Jenkins轻松搞定
  13. 谷歌广告Admob在cocos2dx上通过回调实现底部Banner
  14. JAVAEE——struts2_04:自定义拦截器、struts2标签、登陆功能和校验登陆拦截器的实现
  15. JDK动态代理[4]----ProxyGenerator生成代理类的字节码文件解析
  16. OpenCV OpenGL手写字符识别
  17. [十二省联考2019]字符串问题——后缀自动机+parent树优化建图+拓扑序DP+倍增
  18. verilog 中task用法
  19. 百度翻译爬虫-Web版(自动生成sign)
  20. 搭建TFS 2015 Build Agent环境(四)

热门文章

  1. archetypeCatalog=internal
  2. 新建工程spring boot
  3. 重温HTML和CSS3
  4. Hive SQL语法总结
  5. 家用NAS配置方案
  6. anaconda 安装2个python环境 亲测
  7. Python中的装饰器,迭代器,生成器
  8. 详解linux io flush
  9. Linux下的tar压缩解压命令
  10. Cocos2d 之FlyBird开发---GameUnit类