使用boost库的字符串处理之前,需要进行区域设置。类:std::locale,每个C++程序自动拥有一个此类的实例,不能直接访问全局区域设置。

全局区域设置可以使用类std::locale中的静态函数global()改变。

#include <locale>
#include <iostream> int main() {
std::locale::global(std::locale("German"));
std::locale loc;
std::cout << loc.name() << std::endl;
return ;
}

静态函数global()接受一个类型为std::locale的对象作为其唯一的参数。

正则表达式库 Boost.Regex

boost::regex 用于定义一个正则表达式库

boost::smatch可以保存搜索结果

#include <boost/regex.hpp>
#include <locale>
#include <iostream> int main() {
std::locale::global(std::locale("German"));
std::string s = "Boris Schaling";
boost::regex expr("\\w+\\s\\w+");
 std::cout << boost::regex_match(s, expr) << std::endl;
return 0;
}

boost::regex_match()用于字符串与正则表达式的比较,字符串匹配正则表达式时返回true。

下面介绍一下boost string常用的算法接口:

. case conversiion
to_upper() 将输入字符串转换成大写。
to_upper_copy() 输入字符串不变,返回值为转换成大写的字符串。
to_lower() 将输入字符串转换成小写。
to_lower_copy() 输入字符串不变,返回值为转换成小写的字符串。
. trimming
trim_left() 将输入字符串左边的空格删除。
trim_left_copy() 输入字符串不变,返回去除左边空格的字符串。
trim_left_if() 将输入字符串左边符合条件的字符去除。trim_left_if("12hello", is_digit()), 输出 hello
trim_left_copy_if() 输入字符串不变,意思同上。 trim_right() 意思同left差不多。删除右边的空格
trim_right_if()
trim_right_copy()
trim_right_copy_if() trim() 删除首尾两端的空格。
trim_if()
trim_copy()
trim_copy_if()
. predicates
bool starts_with(input, test) 判断input是否以test为开端。
bool istarts_with(input, test) 判断input是否以test为开端,大小写不敏感。 bool ends_with(input, test) 判断input是否以test结尾。
bool iends_with(input, test) 判断input是否以test结尾, 大小写不敏感。 bool contains(input, test) 判断test是否在input里。
bool icontains(input, test) 判断test是否在input里,大小写不敏感。 bool equals(input, test) 判断input和test是否相等。
bool iequals(input, test) 判断input和test是否相等, 大小写不敏感。 bool lexicographical_compare() 按字典顺序检测input1是否小于input2。
bool ilexicographical_compare() 按字典顺序检测input1是否小于input2, 大小写不敏感。 bool all() 检测输入的每个字符是否符合给定的条件。
. find algorithms
iterator_range<string::iterator> find_first(input, search) 从input中查找第一次出现search的位置。
iterator_range<string::iterator> ifind_first(input, search) 从input中查找第一次出现search的位置,大小写不敏感。 iterator_range<string::iterator> find_last(input, search) 从input中查找最后一次出现search的位置。
iterator_range<string::iterator> ifind_last(input, search) 从input中查找最后一次出现search的位置,大小写不敏感。 iterator_range<string::iterator> find_nth(input, search, n) 从input中查找第n次(n从0开始)出现search的位置。
iterator_range<string::iterator> ifind_nth(input, search, n) 从input中查找第n次(n从0开始)出现search的位置,大小写不敏感。 iterator_range<string::iterator> find_head(input, n) 获取input开头n个字符的字串。
iterator_range<string::iterator> find_tail(input, n) 获取input尾部n个字符的字串。
iterator_range<string::iterator> find_token() 

boost库:字符串处理的更多相关文章

  1. (三)Boost库之字符串处理

    (三)Boost库之字符串处理 字符串处理一直是c/c++的弱项,string_algo库很好的弥补了这一点. string_algo 库算法命名规则: 前缀i    : 有这个前缀表名算法的大小写不 ...

  2. (二)boost库之字符串格式化

    (二)boost库之字符串格式化 程序中经常需要用到字符串格式化,就个人而言还是比较倾向于C格式的输出,如果只是打印日志,printf就够了,如果到生成字符串,获取你可以选择sprintf,但这些都是 ...

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

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

  4. 使用boost库生成 随机数 随机字符串

    #include <iostream> #include <boost/random/random_device.hpp> #include "boost/rando ...

  5. 漫步Facebook开源C++库Folly之string类设计(散列、字符串、向量、内存分配、位处理等,小部分是对现有标准库和Boost库功能上的补充,大部分都是基于性能的需求而“重新制造轮子”)

    就在近日,Facebook宣布开源了内部使用的C++底层库,总称folly,包括散列.字符串.向量.内存分配.位处理等,以满足大规模高性能的需求. 这里是folly的github地址:https:// ...

  6. boost库(条件变量)

    1相关理念 (1)类名 条件变量和互斥变量都是boost库中被封装的类. (2)条件变量 条件变量是thread库提供的一种等待线程同步的机制,可实现线程间的通信,它必须与互斥量配合使用,等待另一个线 ...

  7. boost库学习之regex

    一.背景 项目中许多地方需要对字符串进行匹配,比如根据指定的过滤字符串来过滤文件名.刚开始是排斥使用boost库的,第一,我不熟悉boost库:第二,如果引入第三方库,就会增加库的依赖,这样的后果是, ...

  8. boost库在工作(37)网络UDP服务端之七

    前面介绍的都是网络TCP的服务器和客户端,其实还有UDP的服务器和客户端,同时也有同步和异步之分.UDP与TCP最大的区别,就是TCP是基于连接的,而UDP是无连接的.这里所谓的连接是指对方中断服务时 ...

  9. boost库在工作(39)网络UDP异步服务端之九

    前面创建的UDP服务器和客户端,都是同步的方式,也就是说当接收数据时,不能参与别的事情执行的.如果在一个只有界面线程的程序里,又不想创建多线程,导致复杂程度的增加,在这种情况之下,我们还有一个方案可以 ...

随机推荐

  1. 百度小程序-图片画廊-使用previewImage方法实现

    .swan <!-- 轮播图 S--> <view class="swiper-box"> <swiper style='height:{{swipe ...

  2. yml文件 参数中的逗号 ','

    今天在学习springcloud的geteway的时候,使用yml配置route spring: profiles: betweenroute cloud: gateway: routes: - id ...

  3. Hadoop编程调用HDFS(JAVA)

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 Hadoop环境: Cloudera QuickStart 2.GITHUB地址 http ...

  4. 【BZOJ3473&BZOJ3277】字符串(广义后缀自动机)

    题意:给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串? 本质相同的子串算多个. 对于 100% 的数据,1<=n,k<=10^5,所有字符串总 ...

  5. BZOJ 4710: [Jsoi2011]分特产(容斥)

    传送门 解题思路 首先所有物品是一定要用完的,那么可以按照物品考虑,就是把每种物品分给\(n\)个人,每个人分得非负整数,可以用隔板法计算.设物品有\(m\)个,方案数为\(C(n+m-1,n-1)\ ...

  6. 【ERP知识】一个VMI(供应商管理库存)实现方案

    VMI,Vendor Managed Inventory,供应商管理库存 是指客户不采购或尽量少采购物料,而是由供应商保证该物料有充足的数量,在客户需要的时候能按时提供. 这样可以降低客户方的库存成本 ...

  7. (转)springboot应用启动原理(一) 将启动脚本嵌入jar

    转:https://segmentfault.com/a/1190000013489340 Spring Boot Takes an opinionated view of building prod ...

  8. error C2065: “SHELLEXECUTEINFO”: 未声明的标识符

    转自VC错误:http://www.vcerror.com/?p=1385 问题描述: error C2065: "SHELLEXECUTEINFO": 未声明的标识符 解决方法: ...

  9. Linux下MySQL 命令导入导出sql文件

    导出数据库 直接使用命令: mysqldump -u root -p database >database.sql 然后回车输入密码就可以了: mysqldump -u 数据库链接用户名 -p ...

  10. note《JavaScript 秘密花园》

    点我跳转 (一)JavaScript-Garden-Object (二)JavaScript-Garden-Function (三)JavaScript-Garden-Array (四)JavaScr ...