boost-字符串处理-判断-查找-裁剪-删除-替换-分割-合并

文章目录

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

cpp 复制代码
#include <boost/algorithm/string.hpp>
assert(boost::equals("boost", "boost"));
assert(!boost::equals("boost", "BOOST"));
assert(boost::iequals("boost", "BOOST"));

1.2.all

all , 如果它的所有元素满足一个给定的通过判断式描述的条件,则这个条件式成立。

cpp 复制代码
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

cpp 复制代码
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

cpp 复制代码
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

cpp 复制代码
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:删除输入的结尾。

cpp 复制代码
#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:替换输入的结尾。

cpp 复制代码
#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个子串。

cpp 复制代码
#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这三种方式以及它们的变种版本。

cpp 复制代码
#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(),可以根据某种特定的字符或者策略把分割后的字符,保存到指定的容器中。

cpp 复制代码
#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(),它可以把存储在容器中的字符串通过指定的分隔符连接在一起。

cpp 复制代码
#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.总结

字符串的常用操作,软件工程师使用起来,还是非常方便快捷。

相关推荐
郭源潮11 小时前
【C++二叉树】二叉树的前序遍历、中序遍历、后序遍历递归与非递归实现
开发语言·c++·算法
咩咩大主教2 小时前
LinuxC++的UDP服务器和客户端通信
linux·服务器·c语言·开发语言·c++·udp
zyx没烦恼2 小时前
C\C++内存管理详解
c语言·c++
Wangx_wang2 小时前
C++——多态
开发语言·c++
bcbobo21cn3 小时前
MFC获取网页的html文本
c++·mfc·chttpfile
一丝晨光3 小时前
语言的循环语句
java·c++·python·c#·c·fortran·algol
刘好念3 小时前
[OpenGL]使用OpenGL绘制带纹理三角形
c++·计算机图形学·opengl
为啥不吃肉捏3 小时前
C++/Qt 集成 AutoHotkey
开发语言·c++·qt
努力学习的小廉3 小时前
【C++】—— string模拟实现
开发语言·c++
小灰灰爱代码4 小时前
C++——求3*3矩阵主对角元素之和。
c++·算法·矩阵