boost-字符串处理-判断-查找-裁剪-删除-替换-分割-合并
文章目录
- 1.判断
- 1.1.equals
- 1.2.all
- 1.3.starts_with
- 1.4.ends_with
- 1.5.contains
- 2.大小写转换
- 3.字符串删除
- 4.字符串替换
- 5.字符串查找
- 6.字符串修剪
- 7.字符串分割
- 8.字符串合并
- 9.总结
1.判断
判别式函数和分类函数大多数都是以is_开头,这些函数如下:
判别式函数包括:is_equal(等于)、is_less(小于)、is_not_greater(不大于)、lexicographical_compare(根据顺序检测一个字符串是否小于另一个)、starts_with(检测一个字符串是否是另一个的前缀)、ends_with(检测一个字符串是否是另一个的后缀)、contains(包含)、equals(相等)、all(检测一个字符串中的所有元素是否满足指定的判别式)。
分类函数:is_space、is_alnum(字符和数字)、is_alpha(字母)、is_cntrl(控制符)、is_digit(十进制)、is_graph(图形字符)、is_lower(小写)、is_upper(大写)、is_print(可打印字符)、is_punct(标点符号)、is_xdigit(十六进制)、is_any_of(是否是序列中的任意字符)、if_from_range(是否位于指定区间,包括两头)
1.1.equals
#include <boost/algorithm/string.hpp>
assert(boost::equals("boost", "boost"));
assert(!boost::equals("boost", "BOOST"));
assert(boost::iequals("boost", "BOOST"));
1.2.all
all , 如果它的所有元素满足一个给定的通过判断式描述的条件,则这个条件式成立。
assert(boost::all("\x20\t\n\r", boost::is_space()));
assert(boost::all("\x20\t\n\r", boost::is_classified(std::ctype_base::space)));
assert(boost::all("\x20\t\n\r", boost::is_any_of("\x20\t\n\r")));
assert(boost::all("abcde", boost::is_from_range('a', 'e')));
assert(boost::all("abcde", boost::is_from_range('a', 'z')));
assert(!boost::all("abcde", boost::is_from_range('b', 'c')));
assert(boost::all("abc __ de", boost::is_from_range('a', 'z') || boost::is_space() || boost::is_any_of("_")));
1.3.starts_with
assert(boost::starts_with("boost_python-vc100-mt-1_49.dll", "boost"));
assert(!boost::starts_with("boost_python-vc100-mt-1_49.dll", "BOOST"));
assert(boost::istarts_with("boost_python-vc71-mt-1_33.dll", "BOOST"));
1.4.ends_with
assert(boost::ends_with("boost_python-vc100-mt-1_49.dll", ".dll"));
assert(!boost::ends_with("boost_python-vc100-mt-1_49.dll", ".DLL"));
assert(boost::iends_with("boost_python-vc100-mt-1_49.dll", ".DLL"));
1.5.contains
assert(boost::contains("boost_python-vc100-mt-1_49.dll", "python"));
assert(!boost::contains("boost_python-vc100-mt-1_49.dll", "PYTHON"));
assert(boost::icontains("boost_python-vc100-mt-1_49.dll", "PYTHON"));
is_space: 字符是否为空格
is_alnum: 字符是否为字母和数字字符
is_alpha: 字符是否为字母
is_cntrl: 字符是否为控制字符
is_digit: 字符是否为十进制数字
is_graph: 字符是否为图形字符
is_lower: 字符是否为小写字符
is_print: 字符是否为可打印字符
is_punct: 字符是否为标点符号字符
is_upper: 字符是否为大写字符
is_xdigit: 字符是否为十六进制数字
is_any_of: 字符是否是参数字符序列中的任意字符
is_from_range 字符是否位于指定区间内,即from <= ch <= to
2.大小写转换
主要有如下API:
to_lower_copy:将原来字符串,转换为小写字符串,并返回新的字符串,原来字符串不改变。
to_upper_copy:将原来字符串,转换为大写字符串,并返回新的字符串,原来字符串不改变。
to_lower:将原来字符串,转换为小写字符串,原来字符串改变。
to_upper:将原来字符串,转换为大写字符串,原来字符串改变。
3.字符串删除
boost库提供了众多的字符串删除函数,并且提供了很多版本供使用,例如以i开头的用来区分大小写敏感、以_copy结尾的以不改变原来的字符串等,以满足使用者不同的需求。
erase_first:删除在字符串中第一个出现的字符串。
erase_last:删除在字符串中最后一个出现的字符串。
erase_nth:删除在字符串中第n个出现的字符串。
erase_all:删除在字符串中所有出现的字符串。
erase_head:删除输入的开头。
erase_tail:删除输入的结尾。
#include <iostream>
#include <boost/algorithm/string.hpp>int main()
{std::string tmpStrErase = "Hello!Hello!I'm ISmileLi!Hello!Hello!I'm ISmileLi!";std::cout << "tmpStrErase:" << tmpStrErase << std::endl;boost::algorithm::erase_first(tmpStrErase, "Hello");std::cout << "tmpStrErase:" << tmpStrErase << std::endl;std::string tmpEraseLastStr = boost::algorithm::erase_last_copy(tmpStrErase, "Hello");std::cout << "tmpStrErase:" << tmpStrErase << std::endl;std::cout << "tmpEraseLastStr:" << tmpEraseLastStr << std::endl;boost::algorithm::erase_nth(tmpStrErase, "Hello", 2);std::cout << "tmpStrErase:" << tmpStrErase << std::endl;boost::algorithm::erase_all(tmpStrErase, "Hello");std::cout << "tmpStrErase:" << tmpStrErase << std::endl;std::string tmpEraseHeadCopyStr = boost::algorithm::erase_head_copy(tmpStrErase, 5);std::cout << "tmpStrErase:" << tmpStrErase << std::endl;std::cout << "tmpEraseHeadCopyStr:" << tmpEraseHeadCopyStr << std::endl;std::cout << "Hello World!\n";getchar();
}
4.字符串替换
boost库提供了众多的字符串替换函数,并且提供了很多版本供使用,例如以i开头的用来区分大小写敏感、以_copy结尾的以不改变原来的字符串等,以满足使用者不同的需求。
boost库提供的替换函数如下:
replace_first:替换在字符串中第一个出现的字符串。
replace_last:替换在字符串中最后一个出现的字符串。
replace_nth:替换在字符串中第n个出现的字符串。
replace_all:替换在字符串中所有出现的字符串。
replace_head:替换输入的开头。
replace_tail:替换输入的结尾。
#include <iostream>
#include <boost/algorithm/string.hpp>int main()
{std::cout << "-------------------boost库字符串替换------------------" << std::endl;std::string tmpStrReplace = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";std::cout << "-------------------打印原来字符串的值------------------" << std::endl;std::cout << "tmpStrReplace:" << tmpStrReplace << std::endl;boost::algorithm::replace_first(tmpStrReplace, "HELLO", "hello");std::cout << "-------------------替换第一个字符串后------------------" << std::endl;std::cout << "tmpStrReplace:" << tmpStrReplace << std::endl;std::string tmpReplaceLastStr = boost::algorithm::replace_last_copy(tmpStrReplace, "HELLO", "hello");std::cout << "-------------------替换最后一个字符串后------------------" << std::endl;std::cout << "tmpStrReplace:" << tmpStrReplace << std::endl;std::cout << "tmpReplaceLastStr:" << tmpReplaceLastStr << std::endl;boost::algorithm::replace_nth(tmpStrReplace, "HELLO", 2, "hello");std::cout << "-------------------替换第2个字符串后------------------" << std::endl;std::cout << "tmpStrReplace:" << tmpStrReplace << std::endl;boost::algorithm::replace_all(tmpStrReplace, "HELLO", "hello");std::cout << "-------------------替换所有出现的字符串后------------------" << std::endl;std::cout << "tmpStrReplace:" << tmpStrReplace << std::endl;std::string tmpReplaceTailCopyStr = boost::algorithm::replace_tail_copy(tmpStrReplace, 5, "hello");std::cout << "-------------------替换结尾出现的几个字符串后------------------" << std::endl;std::cout << "tmpStrReplace:" << tmpStrReplace << std::endl;std::cout << "tmpReplaceHeadCopyStr:" << tmpReplaceTailCopyStr << std::endl;std::cout << "Hello World!\n";getchar();
}
5.字符串查找
boost库提供了众多的字符串查找函数,并且提供了很多版本供使用,例如以i开头的用来区分大小写敏感的以不改变原来的字符串等,没有_copy版本,以满足使用者不同的需求。
boost库提供的查找函数如下:
find_first:查找在字符串中第一个出现的字符串。
find_last:查找在字符串中最后一个出现的字符串。
find_nth:查找在字符串中第n个出现的字符串。
find_head:查找输入的开头第N个子串。
find_tail:查找输入的结尾第N个子串。
#include <iostream>
#include <boost/algorithm/string.hpp>int main()
{std::cout << "-------------------boost库字符串查找------------------" << std::endl;std::string tmpStrFind = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";std::cout << "-------------------打印原来字符串的值------------------" << std::endl;std::cout << "tmpStrFind:" << tmpStrFind << std::endl;boost::iterator_range<std::string::iterator> rIter = boost::algorithm::find_first(tmpStrFind, "ISmileLi");std::cout << "查找第一个字符串出现的位置: " << rIter.begin() - tmpStrFind.begin() << std::endl;//迭代器计算位置rIter = boost::algorithm::find_last(tmpStrFind, "ISmileLi");std::cout << "查找最后一个字符串ISmileLi出现的位置: " << rIter.begin() - tmpStrFind.begin() << std::endl;rIter = boost::algorithm::find_nth(tmpStrFind, "HELLO", 3);std::cout << "查找第3个字符串HELLO出现的位置: " << rIter.begin() - tmpStrFind.begin() << std::endl;rIter = boost::algorithm::find_head(tmpStrFind, 3);std::cout << "查找开头3个字符串出现的位置: " << rIter.begin() - tmpStrFind.begin() << std::endl;std::cout << "rIter:" << rIter << std::endl;rIter = boost::algorithm::find_tail(tmpStrFind, 3);std::cout << "查找结尾3个字符串出现的位置: " << rIter.begin() - tmpStrFind.begin() << std::endl;std::cout << "rIter:" << rIter << std::endl;std::cout << "Hello World!\n";getchar();
}
6.字符串修剪
boost库提供了众多的字符串修剪函数,并且提供了很多版本供使用,例如以_copy、_if、_copy_if结尾的版本等,以满足使用者不同的需求。主要函数有trim_left、trim_right、trim这三种方式以及它们的变种版本。
#include <iostream>
#include <boost/algorithm/string.hpp>int main()
{std::cout << "-------------------boost库字符串修剪------------------" << std::endl;std::string tmpTrimSr = " ...88HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!666... ";std::cout << "-------------------打印原来字符串的值------------------" << std::endl;std::cout << "tmpTrimSr:" << tmpTrimSr << std::endl;std::cout << "-------------------删除字符串空格------------------" << std::endl;std::string trimSpace = boost::algorithm::trim_copy(tmpTrimSr);std::cout << "不改变原字符串:" << trimSpace << std::endl;std::cout << "改变原字符串,删除左边:" << std::endl;boost::trim_left(tmpTrimSr);std::cout << tmpTrimSr << std::endl;std::cout << "改变原字符串,删除右边:" << std::endl;boost::trim_right(tmpTrimSr);std::cout << tmpTrimSr << std::endl;std::cout << "-------------------使用判别式删除字符串两端的空格、标点、数字------------------" << std::endl;std::cout << boost::trim_copy_if(tmpTrimSr, boost::is_space() || boost::is_punct() || boost::is_digit()) << std::endl;std::cout << "Hello World!\n";getchar();
}
7.字符串分割
boost库提供了一个分割函数split(),可以根据某种特定的字符或者策略把分割后的字符,保存到指定的容器中。
#include <iostream>
#include <vector>
#include <boost/algorithm/string.hpp>int main()
{std::cout << "-------------------boost库字符串分割------------------" << std::endl;std::string tmpStrFind = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";std::cout << "原字符串:" << tmpStrFind << std::endl;std::vector<std::string> splitVector;// 使用标点符号切割字符串boost::algorithm::split(splitVector, tmpStrFind, boost::algorithm::is_punct());std::cout << "-----------打印切割后的字符串----------" << std::endl;for (int i = 0; i < splitVector.size(); ++i){std::cout << splitVector.at(i) << std::endl;}// 常规用法std::string str = "Hello,World,How,Are,You";std::vector<std::string> result;boost::split(result, str, boost::is_any_of(","));std::cout << "Hello World!\n";getchar();
}
8.字符串合并
boost库提供了一个合并函数join(),它可以把存储在容器中的字符串通过指定的分隔符连接在一起。
#include <iostream>
#include <vector>
#include <boost/algorithm/string.hpp>int main()
{std::cout << "-------------------boost库字符串合并------------------" << std::endl;std::string tmpStrFind = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";std::cout << "原字符串:" << tmpStrFind << std::endl;std::vector<std::string> splitVector;// 使用标点符号切割字符串boost::algorithm::split(splitVector, tmpStrFind, boost::algorithm::is_punct());std::cout << "-----------打印切割后的字符串----------" << std::endl;for (int i = 0; i < splitVector.size(); ++i){std::cout << splitVector.at(i) << std::endl;}std::cout << "-----------使用合并函数join并打印合并后的字符串----------" << std::endl;std::cout << boost::algorithm::join(splitVector, "+") << std::endl;//lambda表达式std::string tempJoinIfStr = boost::algorithm::join_if(splitVector, "$$", [](std::string tmpStr) {return boost::algorithm::contains(tmpStr, "I");});std::cout << "tempJoinIfStr: " << tempJoinIfStr << std::endl;std::cout << "Hello World!\n";getchar();
}
9.总结
字符串的常用操作,软件工程师使用起来,还是非常方便快捷。
相关文章:
boost-字符串处理-判断-查找-裁剪-删除-替换-分割-合并
文章目录 1.判断1.1.equals1.2.all1.3.starts_with1.4.ends_with1.5.contains 2.大小写转换3.字符串删除4.字符串替换5.字符串查找6.字符串修剪7.字符串分割8.字符串合并9.总结 1.判断 判别式函数和分类函数大多数都是以is_开头,这些函数如下: 判别式函…...
Django 开发 web 后端,好用过 SpringBoot ?
基础语法 Django(Python):以简洁和直观著称。它允许更快的开发速度,特别适合快速迭代的项目。例如,一个简单的视图函数: from django.http import HttpResponsedef hello_world(request):return HttpRespon…...
【矩阵】54.螺旋矩阵(顺时针打印矩形元素)
题目 class Solution {public List<Integer> spiralOrder(int[][] matrix) {int m matrix.length, n matrix[0].length;int leftUpM 0, leftUpN 0, rightDownM m - 1, rightDownN n - 1;List<Integer> res new ArrayList<>();while (leftUpM < ri…...
【数据中台】开源项目(5)-Amoro
介绍 Amoro is a Lakehouse management system built on open data lake formats. Working with compute engines including Flink, Spark, and Trino, Amoro brings pluggable and self-managed features for Lakehouse to provide out-of-the-box data warehouse experience,…...
_WorldSpaceLightPos0的含义 UNITY SHADER
_WorldSpaceLightPos0 为当前平行光的方向,方向是从光源到照射的方向。 因此,如果要算法线和平行光之间的夹角, 则需要首先将归一化的_WorldSpaceLightPos0去负数。这样才能继续去计算。 也就是: fixed3 reflectdirnormalize…...
iOS不越狱自动挂机
自动挂机在电脑上或者安卓手机上都相对容易,而在不越狱的iOS设备上还是有点难度的。 此方法不是我原创,详情见: 【苹果党福音,ios也能用的挂机脚本】 https://www.bilibili.com/video/BV1sv4y1P7TL/?share_sourcecopy_web&v…...
智能优化算法应用:基于鼠群算法无线传感器网络(WSN)覆盖优化 - 附代码
智能优化算法应用:基于鼠群算法无线传感器网络(WSN)覆盖优化 - 附代码 文章目录 智能优化算法应用:基于鼠群算法无线传感器网络(WSN)覆盖优化 - 附代码1.无线传感网络节点模型2.覆盖数学模型及分析3.鼠群算法4.实验参数设定5.算法结果6.参考文献7.MATLAB…...
FL Studio中如何录音的技巧,让你的声音更加出众哦!
Hey小伙伴们!今天我要和大家分享一下在FL Studio中如何录音的技巧,让你的声音更加出众哦! 编曲软件FL Studio 即“Fruity Loops Studio ”,也就是众所熟知的水果软件, 全能音乐制作环境或数字音频工作站࿰…...
前端React基础面试题
1,说说react里面bind函数与箭头函数 bind 由于在类中,采用的是严格模式,所以事件回调的时候会丢失this指向,指向的undefined,需要使用bind来给函数绑定上当前实例的this指向。 箭头函数的this指向上下文,所以永久能拿到当前组件实例的。this指向我们可以完美的使用箭头…...
【1day】致远A6系统任意文件下载漏洞学习
注:该文章来自作者日常学习笔记,请勿利用文章内的相关技术从事非法测试,如因此产生的一切不良后果与作者无关。 目录 一、漏洞描述 二、影响版本 三、资产测绘 四、漏洞复现...
朝花夕拾华山平台流水账
2022年8月25日,我加入了诚迈科技(南京),加入了华山平台。 跟我一起入职平台的还有三个小伙伴:小帅、小阳、小甘。 小帅能力很强,前后端都会,入职各种考试工具人。 小阳毕业没多久,一…...
云原生周刊:K8s 的 YAML 技巧 | 2023.12.4
开源项目推荐 Helmfile Helmfile 是用于部署 Helm Chart 的声明性规范。其功能有: 保留图表值文件的目录并维护版本控制中的更改。将 CI/CD 应用于配置更改。定期同步以避免环境偏差。 Docketeer 一款 Docker 和 Kubernetes 开发人员工具,用于管理容…...
Leetcode.2477 到达首都的最少油耗
题目链接 Leetcode.2477 到达首都的最少油耗 rating : 2012 题目描述 给你一棵 n n n 个节点的树(一个无向、连通、无环图),每个节点表示一个城市,编号从 0 0 0 到 n − 1 n - 1 n−1 ,且恰好有 n − 1 n - 1 n−…...
sizeof()、strlen()、length()、size()的区别(笔记)
上面的笔记有点简陋,可以看一下下面这个博主的: c/c中sizeof()、strlen()、length()、size()详解和区别_csize,sizeof,length_xuechanba的博客-CSDN博客...
Redis击穿(热点key失效)
Redis击穿是指在高并发情况下,一个键在缓存中过期失效时,同时有大量请求访问该键,导致所有请求都落到数据库上,对数据库造成压力。这种情况下,数据库可能无法及时处理这些请求,导致性能下降甚至崩溃。 为了…...
分类预测 | Matlab实现OOA-CNN-SVM鱼鹰算法优化卷积支持向量机分类预测
分类预测 | Matlab实现OOA-CNN-SVM鱼鹰算法优化卷积支持向量机分类预测 目录 分类预测 | Matlab实现OOA-CNN-SVM鱼鹰算法优化卷积支持向量机分类预测分类效果基本描述程序设计参考资料 分类效果 基本描述 1.Matlab实现OOA-CNN-SVM鱼鹰算法优化卷积支持向量机分类预测࿰…...
class文件结构
文章目录 1. 常量池集合2. 访问标志3. 字段表集合4. 方法表集合5. 属性表集合 成员变量(非静态)的赋值过程:1. 默认初始化 2. 显示初始化/代码块中初始化 3. 构造器中初始化 4. 有了对象后对象。属性或者对象。方法的方式对成员变量进行赋值 …...
多重背包问题 一句话说清楚“二进制拆分“
目录 区别: 一句话说清楚: 板子: 区别: 得先懂完全背包问题完全背包问题 非零基础-CSDN博客 都是让背包内价值最大。 完全背包问题每种物品可以取无数次。而多重背包问题每件取的次数有限。 都可以用的最挫的方法就是0~k件去…...
nodejs微信小程序+python+PHP本科生优秀作业交流网站的设计与实现-计算机毕业设计推荐
通过软件的需求分析已经获得了系统的基本功能需求,根据需求,将本科生优秀作业交流网站功能模块主要分为管理员模块。管理员添加系统首页、个人中心、用户管理、作业分类管理、作业分享管理、论坛交流、投诉举报、系统管理等操作。 随着信息化社会的形成…...
使用git出现的问题
保证 首先保证自己的git已经下载 其次保证自己的gitee账号已经安装并且已经生成ssh公钥 保证自己要push的代码在要上传的文件夹内并且配置文件等都在父文件夹(也就是文件没有套着文件) 问题 1 $ git push origin master gitgitee.com: Permission de…...
CVPR 2025 MIMO: 支持视觉指代和像素grounding 的医学视觉语言模型
CVPR 2025 | MIMO:支持视觉指代和像素对齐的医学视觉语言模型 论文信息 标题:MIMO: A medical vision language model with visual referring multimodal input and pixel grounding multimodal output作者:Yanyuan Chen, Dexuan Xu, Yu Hu…...
突破不可导策略的训练难题:零阶优化与强化学习的深度嵌合
强化学习(Reinforcement Learning, RL)是工业领域智能控制的重要方法。它的基本原理是将最优控制问题建模为马尔可夫决策过程,然后使用强化学习的Actor-Critic机制(中文译作“知行互动”机制),逐步迭代求解…...
Zustand 状态管理库:极简而强大的解决方案
Zustand 是一个轻量级、快速和可扩展的状态管理库,特别适合 React 应用。它以简洁的 API 和高效的性能解决了 Redux 等状态管理方案中的繁琐问题。 核心优势对比 基本使用指南 1. 创建 Store // store.js import create from zustandconst useStore create((set)…...
ardupilot 开发环境eclipse 中import 缺少C++
目录 文章目录 目录摘要1.修复过程摘要 本节主要解决ardupilot 开发环境eclipse 中import 缺少C++,无法导入ardupilot代码,会引起查看不方便的问题。如下图所示 1.修复过程 0.安装ubuntu 软件中自带的eclipse 1.打开eclipse—Help—install new software 2.在 Work with中…...
[Java恶补day16] 238.除自身以外数组的乘积
给你一个整数数组 nums,返回 数组 answer ,其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积 。 题目数据 保证 数组 nums之中任意元素的全部前缀元素和后缀的乘积都在 32 位 整数范围内。 请 不要使用除法,且在 O(n) 时间复杂度…...
R 语言科研绘图第 55 期 --- 网络图-聚类
在发表科研论文的过程中,科研绘图是必不可少的,一张好看的图形会是文章很大的加分项。 为了便于使用,本系列文章介绍的所有绘图都已收录到了 sciRplot 项目中,获取方式: R 语言科研绘图模板 --- sciRplothttps://mp.…...
tomcat入门
1 tomcat 是什么 apache开发的web服务器可以为java web程序提供运行环境tomcat是一款高效,稳定,易于使用的web服务器tomcathttp服务器Servlet服务器 2 tomcat 目录介绍 -bin #存放tomcat的脚本 -conf #存放tomcat的配置文件 ---catalina.policy #to…...
wpf在image控件上快速显示内存图像
wpf在image控件上快速显示内存图像https://www.cnblogs.com/haodafeng/p/10431387.html 如果你在寻找能够快速在image控件刷新大图像(比如分辨率3000*3000的图像)的办法,尤其是想把内存中的裸数据(只有图像的数据,不包…...
沙箱虚拟化技术虚拟机容器之间的关系详解
问题 沙箱、虚拟化、容器三者分开一一介绍的话我知道他们各自都是什么东西,但是如果把三者放在一起,它们之间到底什么关系?又有什么联系呢?我不是很明白!!! 就比如说: 沙箱&#…...
基于单片机的宠物屋智能系统设计与实现(论文+源码)
本设计基于单片机的宠物屋智能系统核心是实现对宠物生活环境及状态的智能管理。系统以单片机为中枢,连接红外测温传感器,可实时精准捕捉宠物体温变化,以便及时发现健康异常;水位检测传感器时刻监测饮用水余量,防止宠物…...
