boost常用字符串处理方法学习

工作中经常要用到boost中关于字符串处理的方法,这里做个简单的介绍:

分割字符串:split

	string test1("Hello world, hello programmer");
	vector vec1;
	boost::split(vec1, test1, boost::is_any_of(", "));//支持同时使用多个分割符
	for (vector::const_iterator ite = vec1.begin(); ite != vec1.end(); ++ite)
	{3
		cout << *ite << endl;
	}


输出:
Hello
world

hello
programmer

替换:replace(支持中文替换)

	string test4("这封邮件是来自新西兰的!是我的朋友jim发给我的");
	string str1("新西兰");
	string str2("荷兰");
	boost::replace_first(test4, str1, str2);
	cout << test4 << endl;

输出:

这封邮件是来自荷兰的!是我的朋友jim发给我的

修剪(trim):trim

	string test2("test string ");
	cout << boost::trim_right_copy(test2) << endl;
	string test3("test string;");
	cout << boost::trim_right_copy_if(test2, boost::is_any_of(";")) << endl;


输出:
test string
test string


也可以直接在原始字符串上进行trimming,在一系列trim的方法中,方法名中没有“copy”这个单词的就可以 


合并:join

	string test5("未来总比");
	string test6("现在好");
	//vector vec2{test5, test6};
	vector vec2;
	vec2.push_back(test5);
	vec2.push_back(test6);

	string test7 = boost::join(vec2, string("\t"));
	cout << test7 << endl;

输出:

未来总比	现在好

boost中关于字符串处理的方法还有很多,具体可以参考boost string_algo部分的手册。下面介绍一下string_algo的命名以便阅读手册时候知道方法大致功能:

boost常用字符串处理方法学习_第1张图片

(上述内容出自《boost程序库完全开发指南》)


你可能感兴趣的:(c++)