Boost 入门02(字符串操作)

2 篇文章 0 订阅

字符串操作

使用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(). 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值