boost库中的字符串算法查找、切割、替换、删除、合并、大小写转换等等详解

零、小序

字符串操作是每一个程序员几乎每天都要进行的工作,很多的开发工作中都要用到字符串的操作,无论是网络通讯、界面显示还是写日志等操作,大多数程序员都喜欢搞成字符串的形式来进行操作。标准的C++对字符串的操作已经提供了很多方法,但还是有很多不尽人意的地方,例如对字符串切割,大小写转换、替换、删除等等操作,使用boost进行处理会更加的方便,boost库提供的支持更加的齐全,没有boost库做不到的,只有你想不到的!

一、大小写转换

1、大小写转换函数

boost提供的大小写转换函数有boost::algorithm::to_upper_copy和boost::algorithm::to_lower_copy、boost::algorithm::to_upper和boost::algorithm::to_lower这两组大小写转换的形式。前一组的大小写转换不会改变原来的字符串,会返回一个原来字符串的拷贝,后面一组大小写转换会改变原来的字符串的大小写。

细心的朋友会发现上面这两组大小写转换会提供的有两个参数,第一个是传入要转换的字符串,第二个是有关区域设置的,这个区域设置一般默认就行,除非你开发的软件具有区域性,比如开发的软件给外国人使用,你可能需要设置成那个国家的区域标志,以适合那个国家的文化需要。

2、大小写代码示例

// BoostStringAlgorithms.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <boost/algorithm/string.hpp>

using namespace std;
using namespace boost;

int main()
{
	cout << "-------------------boost库大小写转换------------------" << endl;
	string tmpStrUpper = "HELLO,I'M ISMILELI";
	string tmpStrLower = "hello,i'm ismileli";

	cout << "-------------------打印原来字符串的值------------------" << endl;
	cout << "tmpStrUpper:" << tmpStrUpper << endl;
	cout << "tmpStrLower:" << tmpStrLower << endl;

	cout << "-------------------使用*_copy转换大小写------------------" << endl;
	string upperToLowerStr = boost::algorithm::to_lower_copy(tmpStrUpper);
	string lowerToUpperStr = boost::algorithm::to_upper_copy(tmpStrLower);
	cout << "tmpStrUpper:" << tmpStrUpper << endl;
	cout << "tmpStrLower:" << tmpStrLower << endl;
	cout << "upperToLowerStr:" << upperToLowerStr << endl;
	cout << "lowerToUpperStr:" << lowerToUpperStr << endl;

	cout << "-------------------使用不带后缀*_copy转换大小写------------------" << endl;
	boost::algorithm::to_lower(tmpStrUpper); // 没有返回值
	boost::algorithm::to_upper(tmpStrLower);
	cout << "tmpStrUpper:" << tmpStrUpper << endl;
	cout << "tmpStrLower:" << tmpStrLower << endl;

    std::cout << "Hello World!\n";
	getchar();
}

运行结果:
在这里插入图片描述

二、删除

1、删除函数

boost库提供了众多的字符串删除函数,并且提供了很多版本供使用,例如以i开头的用来区分大小写敏感、以_copy结尾的以不改变原来的字符串等,以满足使用者不同的需求。

boost库提供的删除函数如下:
erase_first:删除在字符串中第一个出现的字符串。
erase_last:删除在字符串中最后一个出现的字符串。
erase_nth:删除在字符串中第n个出现的字符串。
erase_all:删除在字符串中所有出现的字符串。
erase_head:删除输入的开头。
erase_tail:删除输入的结尾。

2、删除代码示例

// BoostStringAlgorithms.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <boost/algorithm/string.hpp>

using namespace std;
using namespace boost;

int main()
{
	cout << "-------------------boost库字符串删除------------------" << endl;
	string tmpStrErase = "Hello!Hello!I'm ISmileLi!Hello!Hello!I'm ISmileLi!";
	cout << "-------------------打印原来字符串的值------------------" << endl;
	cout << "tmpStrErase:" << tmpStrErase << endl;

	boost::algorithm::erase_first(tmpStrErase,"Hello");
	cout << "-------------------删除第一个字符串后------------------" << endl;
	cout << "tmpStrErase:" << tmpStrErase << endl;

	string tmpEraseLastStr = boost::algorithm::erase_last_copy(tmpStrErase, "Hello");
	cout << "-------------------删除最后一个字符串后------------------" << endl;
	cout << "tmpStrErase:" << tmpStrErase << endl;
	cout << "tmpEraseLastStr:" << tmpEraseLastStr << endl;

	boost::algorithm::erase_nth(tmpStrErase, "Hello",2);
	cout << "-------------------删除第2个字符串后------------------" << endl;
	cout << "tmpStrErase:" << tmpStrErase << endl;

	boost::algorithm::erase_all(tmpStrErase, "Hello");
	cout << "-------------------删除所有出现的字符串后------------------" << endl;
	cout << "tmpStrErase:" << tmpStrErase << endl;

	string tmpEraseHeadCopyStr = boost::algorithm::erase_head_copy(tmpStrErase,5);
	cout << "-------------------删除开头出现的几个字符串后------------------" << endl;
	cout << "tmpStrErase:" << tmpStrErase << endl;
	cout << "tmpEraseHeadCopyStr:" << tmpEraseHeadCopyStr << endl;

	std::cout << "Hello World!\n";
	getchar();
}

运行结果:
在这里插入图片描述

三、替换

1、替换函数

boost库提供了众多的字符串替换函数,并且提供了很多版本供使用,例如以i开头的用来区分大小写敏感、以_copy结尾的以不改变原来的字符串等,以满足使用者不同的需求。

boost库提供的替换函数如下:
replace_first:替换在字符串中第一个出现的字符串。
replace_last:替换在字符串中最后一个出现的字符串。
replace_nth:替换在字符串中第n个出现的字符串。
replace_all:替换在字符串中所有出现的字符串。
replace_head:替换输入的开头。
replace_tail:替换输入的结尾。

2、替换代码示例

// BoostStringAlgorithms.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <boost/algorithm/string.hpp>

using namespace std;
using namespace boost;

int main()
{
	cout << "-------------------boost库字符串替换------------------" << endl;
	string tmpStrReplace = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";
	cout << "-------------------打印原来字符串的值------------------" << endl;
	cout << "tmpStrReplace:" << tmpStrReplace << endl;

	boost::algorithm::replace_first(tmpStrReplace, "HELLO","hello");
	cout << "-------------------替换第一个字符串后------------------" << endl;
	cout << "tmpStrReplace:" << tmpStrReplace << endl;

	string tmpReplaceLastStr = boost::algorithm::replace_last_copy(tmpStrReplace, "HELLO","hello");
	cout << "-------------------替换最后一个字符串后------------------" << endl;
	cout << "tmpStrReplace:" << tmpStrReplace << endl;
	cout << "tmpReplaceLastStr:" << tmpReplaceLastStr << endl;

	boost::algorithm::replace_nth(tmpStrReplace, "HELLO", 2, "hello");
	cout << "-------------------替换第2个字符串后------------------" << endl;
	cout << "tmpStrReplace:" << tmpStrReplace << endl;

	boost::algorithm::replace_all(tmpStrReplace, "HELLO", "hello");
	cout << "-------------------替换所有出现的字符串后------------------" << endl;
	cout << "tmpStrReplace:" << tmpStrReplace << endl;

	string tmpReplaceTailCopyStr = boost::algorithm::replace_tail_copy(tmpStrReplace, 5, "hello");
	cout << "-------------------替换结尾出现的几个字符串后------------------" << endl;
	cout << "tmpStrReplace:" << tmpStrReplace << endl;
	cout << "tmpReplaceHeadCopyStr:" << tmpReplaceTailCopyStr << endl;


	std::cout << "Hello World!\n";
	getchar();
}

运行结果:
在这里插入图片描述

四、查找

1、查找函数

boost库提供了众多的字符串查找函数,并且提供了很多版本供使用,例如以i开头的用来区分大小写敏感的以不改变原来的字符串等,没有_copy版本,以满足使用者不同的需求。

boost库提供的查找函数如下:
find_first:查找在字符串中第一个出现的字符串。
find_last:查找在字符串中最后一个出现的字符串。
find_nth:查找在字符串中第n个出现的字符串。
find_head:查找输入的开头第N个子串。
find_tail:查找输入的结尾第N个子串。

2、查找代码示例

// BoostStringAlgorithms.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <boost/algorithm/string.hpp>

using namespace std;
using namespace boost;

int main()
{
	cout << "-------------------boost库字符串查找------------------" << endl;
	string tmpStrFind = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";
	cout << "-------------------打印原来字符串的值------------------" << endl;
	cout << "tmpStrFind:" << tmpStrFind << endl;
	boost::iterator_range<string::iterator> rIter = boost::algorithm::find_first(tmpStrFind, "ISmileLi");
	cout << "查找第一个字符串出现的位置: " << rIter.begin()-tmpStrFind.begin() << endl;

	rIter = boost::algorithm::find_last(tmpStrFind, "ISmileLi");
	cout << "查找最后一个字符串ISmileLi出现的位置: " << rIter.begin() - tmpStrFind.begin() << endl;

	rIter = boost::algorithm::find_nth(tmpStrFind, "HELLO",3);
	cout << "查找第3个字符串HELLO出现的位置: " << rIter.begin() - tmpStrFind.begin() << endl;

	rIter = boost::algorithm::find_head(tmpStrFind, 3);
	cout << "查找开头3个字符串出现的位置: " << rIter.begin() - tmpStrFind.begin() << endl;
	cout << "rIter:" << rIter << endl;

	rIter = boost::algorithm::find_tail(tmpStrFind, 3);
	cout << "查找结尾3个字符串出现的位置: " << rIter.begin() - tmpStrFind.begin() << endl;
	cout << "rIter:" << rIter << endl;

	std::cout << "Hello World!\n";
	getchar();
}

运行结果:
在这里插入图片描述

五、判别式和分类

1、判别式和分类函数

boost库提供了一组判别式函数用来判断大小、等于关系,分类函数用来分类哪些是数字、哪些是字符、哪些是空格、哪些是十进制等等,检测一个字符是否是某种特性。

判别式函数和分类函数大多数都是以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(是否位于指定区间,包括两头)

2、判别式和分类代码示例

使用比较简单,不再单独示例,可以看下一节的代码示例

六、修剪

1、修剪函数

boost库提供了众多的字符串修剪函数,并且提供了很多版本供使用,例如以_copy、_if、_copy_if结尾的版本等,以满足使用者不同的需求。

主要函数有trim_left、trim_right、trim这三种方式以及它们的变种版本。

2、修剪代码示例

// BoostStringAlgorithms.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <boost/algorithm/string.hpp>

using namespace std;
using namespace boost;

int main()
{
	cout << "-------------------boost库字符串修剪------------------" << endl;
	string tmpTrimSr = "  ...88HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!666...  ";
	cout << "-------------------打印原来字符串的值------------------" << endl;
	cout << "tmpTrimSr:" << tmpTrimSr << endl;

	cout << "-------------------删除字符串空格------------------" << endl;
	string trimSpace = boost::algorithm::trim_copy(tmpTrimSr);
	cout << "不改变原字符串:" << trimSpace << endl;

	cout << "改变原字符串,删除左边:" << endl;
	boost::trim_left(tmpTrimSr);
	cout << tmpTrimSr << endl;

	cout << "改变原字符串,删除右边:" << endl;
	boost::trim_right(tmpTrimSr);
	cout << tmpTrimSr << endl;

	cout << "-------------------使用判别式删除字符串两端的空格、标点、数字------------------" << endl;
	cout << boost::trim_copy_if(tmpTrimSr, is_space() || is_punct() || is_digit()) << endl;

	std::cout << "Hello World!\n";
	getchar();
}

运行结果:
在这里插入图片描述

七、分割

1、分割函数

boost库提供了一个分割函数split(),可以根据某种特定的字符或者策略把分割后的字符,保存到指定的容器中。

2、分割代码示例

// BoostStringAlgorithms.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <vector>
#include <boost/algorithm/string.hpp>

using namespace std;
using namespace boost;

int main()
{
	cout << "-------------------boost库字符串分割------------------" << endl;
	string tmpStrFind = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";
	cout << "原字符串:" << tmpStrFind << endl;

	vector<std::string> splitVector;
	// 使用标点符号切割字符串
	boost::algorithm::split(splitVector, tmpStrFind, boost::algorithm::is_punct());
	cout << "-----------打印切割后的字符串----------" << endl;
	for (int i=0; i<splitVector.size(); ++i)
	{
		cout << splitVector.at(i) << endl;
	}

	std::cout << "Hello World!\n";
	getchar();
}

运行结果:
在这里插入图片描述

八、合并

1、合并函数

boost库提供了一个合并函数join(),它可以把存储在容器中的字符串通过指定的分隔符连接在一起。

2、合并代码示例

// BoostStringAlgorithms.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <vector>
#include <boost/algorithm/string.hpp>

using namespace std;
using namespace boost;

int main()
{
	cout << "-------------------boost库字符串合并------------------" << endl;
	string tmpStrFind = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";
	cout << "原字符串:" << tmpStrFind << endl;

	vector<std::string> splitVector;
	// 使用标点符号切割字符串
	boost::algorithm::split(splitVector, tmpStrFind, boost::algorithm::is_punct());
	cout << "-----------打印切割后的字符串----------" << endl;
	for (int i = 0; i < splitVector.size(); ++i)
	{
		cout << splitVector.at(i) << endl;
	}

	cout << "-----------使用合并函数join并打印合并后的字符串----------" << endl;

	cout << boost::algorithm::join(splitVector, "+") << endl;

	string tempJoinIfStr = boost::algorithm::join_if(splitVector, "$$", [](string tmpStr){
		return boost::algorithm::contains(tmpStr, "I");
	});

	cout << "tempJoinIfStr: " << tempJoinIfStr << endl;
	std::cout << "Hello World!\n";
	getchar();
}

运行结果:
在这里插入图片描述

九、题外话

1、boost提供的字符串操作还有很多,无法一一举例列出,更多使用方法还需要自己多多研究一下。

2、更多boost库信息请移步这里:https://blog.csdn.net/toby54king/category_7762600.html

3、这篇文章写了整整4个多小时,实在不容易啊,希望各位老铁多多支持!

原创不易,点赞鼓励一下吧!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章