C++:boost::string中的字符处理函数

一、字符串处理函数

许多函数都有忽略字符串大小写的版本, 这些版本一般都有与原函数相似的名称,所相差的只是以 ‘i’.开头。 例如,与函数 boost::algorithm::erase_all_copy()相对应的是函数boost::algorithm::ierase_all_copy()

1:大小写转换

下面函数均是传值,因此返回一个新的字符串,而不是传引用,所以并不改变原字符串。
to_upper_copy()
to_lower_copy()
如果作为参数传入的字符串自身需要被转化为大小写,则可以使用:
to_upper()
to_lower()

2:删除字符串中的字符

erase_first_copy(s,"i")
erase_nth_copy(s,"i",index),index表示要删除的下标
erase_last_copy()
erase_all_copy()
erase_head_copy()
erase_tail_copy()

3:查找字符串

find_first()
find_last()
find_nth()
find_head()
find_tail()

4:连接字符串

join(s," ")

5:替换

replace_first_copy(s,source_string,target_string)
replace_last_copy()
replace_nth_copy()
replace_all_copy()
replace_head_copy()
replace_tail_copy()

6:修剪

去除空格
trim_left_copy()
trim_right_copy()
trim_copy()
去除指定字符
trim_left_copy_if()
trim_right_copy_if()
trim_copy_if()

is_any_of():是指定字符串中的任意字符
is_digit():是数字

7:比较

starts_with()
ends_with()
contains()
lexicographical_compare()

8:字符串切割

将一个字符串拆分为一个字符串容器。
spit(字符串容器,被切割字符串,切割谓词)
e.g:

vector<string> v;
split(v,s,is_space());

第三个参数必须是一个函数(返回值为bool),而不能是一个字符串。

二、正则表达式库boost.regex

regex expr(匹配规则字符串);
regex_match(str, expr);

regex_match()用于字符串与正则表达式的比较,符合时返回true;
regex_search()在字符串中搜索正则表达式

regex expr();
smath what;
regex_search(str,what,expr);

regex_replace()在整个字符串中搜索正则表达式,然后替换

regex expr();
string fmt;
regex_replace(str,expr,fmt);

find_regex()
erase_regex()
split_regex()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值