没什么说的,需要

#include<boost/algorithm/string.hpp>

1.大小写转换

    std::string s("test string");

    boost::to_upper(s);//转换为大写
boost::to_lower(s);//转换为小写 std::string str1=boost::to_lower_copy(s);//小写转换并赋值
std::string str2=boost::to_upper_copy(s);//大写转换并赋值
    std::array<string, 3> k = {"hello", "world", "123"};
std::cout << join(k, "-"); //输出结果为: hello-world-123

2.分割与合并字符串

    std::string s("test stringa-test stringb-test stringc");
std::vector<std::string> sv;
boost::split(sv,s,boost::is_any_of("-"),boost::token_compress_on);
//Now,sv={"test stringa","test stringb","test stringc"};

3.去掉字符串两边空格

    std::string s("      test string      ");
boost::trim_left(s);//去掉字符串左边空格
boost::trim_right(s);//去掉字符串右边空格
//现在s="test string"
//boost::trim_left_copy(s)和boost::trim_right_copy(s)表示去掉后赋值

trim_left_copy_if() 将字符串开头的符合我们提供的“谓词”的特定字符去掉,并且赋值给另一个字符串

 string str1(" hello world! ");
string str2;
str2 = trim_left_copy_if(str1, NotH); // str2 == "ello world! "

总结一下就是凡是有copy就是指向后赋值,有if就判断谓词

3.谓词

    std::string s("test string");
boost::starts_with("test");//判断字符串是否以一个字符串开始,返回bool
    std::string a("sss");
std::string b("sss");
boost::equal(a,b);//判断字符串是否完全匹配
    std::string s("test string");
boost::contains("te");//判断字符串是否含有某字符串
boost::ends_with("ing");//判断字符串是否以另一个字符串结尾;
// boost::iends_with()同上只是不区分大小写

all()判断字符串中的所有字符是否全部满足这个谓词

    std::string s("test string");

    bool str_equal(const std::string p){
if(boost::equal(p,"test string"))
return true;
return false;
}
boost::all(s,str_equal);

4.查找字符串

这里复制粘贴一段

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!"

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

find_last() 从尾查找字符串中的子字符串,返回这个子串在原串中的iterator_range迭代器

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

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!"

ifind_nth() 找到第n个匹配的子串(计算从0开始)(不区分大小写)

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!"

find_tail() 找到字符串的后n个字节

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

5.替换/删除字符串

replace_first() 从头找到第一个匹配的字符串,将其替换为给定的另外一个字符串

 Example:

 string str1("hello world!");

 replace_first(str1, "hello", "Hello"); // str1 = "Hello world!"

replace_first_copy()  从头找到第一个匹配的字符串,将其替换为给定的另外一个字符串,并且赋

值给另一个字符串

 Example:

 string str1("hello world!");

 string str2;

 str2 = replace_first_copy(str1, "hello", "Hello"); // str2 = "Hello world!"

ireplace_first()  从头找到第一个匹配的字符串,将其替换为给定的另外一个字符串(不区分大小写

)

ireplace_first_copy()  从头找到第一个匹配的字符串,将其替换为给定的另外一个字符串,并且赋

值给另一个字符串(不区分大小写)

erase_first()   从头找到第一个匹配的字符串,将其删除

 Example:

 string str1("hello world!");

 erase_first(str1, "llo"); // str1 = "He world!"

erase_first_copy() 从头找到第一个匹配的字符串,将其删除,并且赋值给另一个字符串

 Example:

 string str1("hello world!");

 string str2;

 str2 = erase_first_copy(str1, "llo"); // str2 = "He world!"

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

boost::algorithm(字符串算法库)的更多相关文章

  1. boost::format(字符串格式化库)

    这段时间学习boost库的使用,撰文一方面留以备用,另一方面就是shared精神. format主要是用来格式化std::string字符串以及配合std::cout代替C语言printf() 使用f ...

  2. boost字符串算法

    boost::algorithm简介 2007-12-08 16:59 boost::algorithm提供了很多字符串算法,包括: 大小写转换: 去除无效字符: 谓词: 查找: 删除/替换: 切割: ...

  3. C++神奇算法库——#include<algorithm>

    算法(Algorithm)为一个计算的具体步骤,常用于计算.数据处理和自动推理.C++ 算法库(Algorithms library)为 C++ 程序提供了大量可以用来对容器及其它序列进行算法操作的函 ...

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

    http://blog.csdn.net/qingzai_/article/details/44417937 下面先列举几个常用的: #define i_end_with boost::iends_w ...

  5. C++ algorithm算法库

    C++ algorithm算法库 Xun 标准模板库(STL)中定义了很多的常用算法,这些算法主要定义在<algorithm>中.编程时,只需要在文件中加入#include<algo ...

  6. 字符串算法(string_algorithm)

    format 作用 格式化输出对象,可以不改变流输出状态实现类似于printf()的输出 头文件 #include <boost/format.hpp> using namespace b ...

  7. OpenSSL密码算法库: MD5示例小程序

    OpenSSL http://www.openssl.org/ OpenSSL整个软件包大概可以分成三个主要的功能部分:密码算法库.SSL协议库以及应用程序.OpenSSL 的密码算法库包含多种加密算 ...

  8. C++的字符串格式化库

    这里向大家介绍一个C++的字符串格式化库,叫cpptempl,这个库支持对字符串格式的条件,循环,变量插入.看上去很不错,只不过其是基于boost库的. 下面是一个例子: 1 2 3 4 5 6 7 ...

  9. boost split字符串

    boost split string , which is very convenience #include <string> #include <iostream> #in ...

随机推荐

  1. Python 日志处理(一) 按Nginx log_format 分割日志记录

    要求:不使用正则 根据nginx 默认的日志记录格式,分割日志记录. log_format main '$remote_addr - $remote_user [$time_local] " ...

  2. struts2对于处理JSON的配置

    由于最近几年日益流行前后端分离模式,JSON作为数据载体也变得不可或缺.几乎所有的web框架都需要支持JSON,下面咱就一起了解下struts2是如何支持JSON的. 对于JSON的发送 这里有两种方 ...

  3. 条件随机场 Conditional Random Fields

    简介 假设你有冠西哥一天生活中的照片(这些照片是按时间排好序的),然后你很无聊的想给每张照片打标签(Tag),比如这张是冠西哥在吃饭,那张是冠西哥在睡觉,那么你该怎么做呢? 一种方法是不管这些照片的序 ...

  4. react入门到进阶(一)

    一.何为react Facebook在F8会议上首次提出这个概念,一套全新的框架就此诞生. React 不是一个完整的 MVC.MVVM 框架,其只负责 View 层 React 跟 Web Comp ...

  5. 树莓派远程桌面配置-开机自启SSH

    必须先安装tightvncserver sudo apt-get install tightvncserver 再安装xrdp服务. sudo apt-get install xrdp 如果开着防火墙 ...

  6. zanphp 初探----安装篇

    安装 zanphp 的安装详细步骤具体在 http://zanphpdoc.zanphp.io/,但是安装的时候,还是踩了一些坑,Mac 和 Ubuntu 我都安装过, 分享大家注意一下. PHP 版 ...

  7. Javascript 数组(Array)相关内容总结

    创建数组 var colors = new Array(); //创建新数组 var num = new Array(3); //创建包含三项的新数组 var names = new Array(&q ...

  8. MySQL服务找不到了,navicat打不开数据库连接

    今天打开Navicat看看连接名,突然发现连接不上了,打开服务发现MySQL服务不见了,所以手动安装了遍MySQL服务. 详细步骤如下: 1.管理员身份打开cmd,切换到MySQL安装目录下的bin目 ...

  9. PostgreSQL索引描述

    索引方式:唯一索引,主键索引,多属性索引,部分索引,表达式索引. 索引类型:B-Tree,Hash,GiST,GIN以及表达式索引 PostgreSQL所有索引都是“从属索引”,也就是说,索引在物理上 ...

  10. 阿里云服务器部署php的laravel项目,在阿里云买ECS 搭建 Linux+Nginx+Mysql+PHP环境的

    在阿里云买ECS的时候选择自己习惯的镜像系统,我一般都是使用Linux Ubuntu,所以,以下的配置都是在Ubuntu 14.04稳定支持版的环境中搭建Linux+Nginx+Mysql+PHP环境 ...