您的位置:首页 > 其它

<Boost> 字符串处理和格式化输出

2015-01-06 11:38 441 查看
boost有丰富的字符串处理和格式化输出库。

boost::string_algo

用于字符串大小写转换,比较,修剪和查找

boost::format

用于字符串格式化输出,如printf一般.

boost::tokenizer

用于字符串分割

下面是测试代码:

#include <boost/algorithm/string.hpp>
#include <boost/format.hpp> 
#include <boost/tokenizer.hpp>

void TestStringFormat()
{
	//////////////////////////////////////////////////////////////////////////
	// 字符串处理和格式化输出
	//////////////////////////////////////////////////////////////////////////
	// string_algo: 字符串大小写转换,比较,修剪,特定字符串的查找
	string str1 = "  ..this iS My String";
	cout << "Original : " << str1 << endl;
	cout << "End With \"ing\": " << boost::ends_with(str1, "ing")  << endl;	// 判断是否以"ing"结尾
	cout << "After to upper : " << boost::to_upper_copy(str1) << endl;;		// 转换为大写, _copy:不改变原字符串
	cout << "Trim: " << boost::trim_left_copy(str1) << endl;;			// 删除左侧的空白字符
	boost::replace_first(str1, "this", "thez");			// 替换"this"为"thez"
	cout << "After replace: " << str1  << endl;

	boost::iterator_range<std::string::iterator> ir = boost::find_first(str1, "ing");
	cout << "Find \"ing\": " << string(ir.begin(), ir.end()) << endl;

	vector<char> v(str1.begin(),str1.end());
	vector<char> v2= boost::to_upper_copy(boost::erase_first_copy(v, "String")); // 删除"String"部分之后转换为大写字符,赋值v2
	for(int i=0; i<v2.size();++i)
	{
		cout << v2[i];
	}
	cout << endl;

	//////////////////////////////////////////////////////////////////////////
	// format: 格式化输出
	cout <<  
		boost::format("date: %05d, %.2f, %s") 
		% 2304
		% 343.2
		% "my"
		<<endl; 

	//////////////////////////////////////////////////////////////////////////
	// tokenizer 分割
	string strLine = "this is the\ntest\tline for,tokenizer.hello";
	boost::tokenizer<> tok(strLine);	// 缺省的情况下,以空白字符,标点作为分隔字符

	for (boost::tokenizer<>::iterator it = tok.begin(); it != tok.end(); it++)
	{
		cout << *it << "__";
	}
	cout << endl;
	// char_separator
	boost::char_separator<char> cs;	
	boost::tokenizer<boost::char_separator<char> > tokcs(strLine, cs); // 默认保留标点但将它看作分隔符

	for (boost::tokenizer<boost::char_separator<char> >::iterator it = tokcs.begin(); it != tokcs.end(); it++)
	{
		cout << *it << "__";
	}
	cout << endl;

	// char_separator自定义
	string strLine1 = "this|is|the-test*Line for,tokenizer";
	boost::char_separator<char> custcs("|-*");	// 自己指定
	boost::tokenizer<boost::char_separator<char> > tokcs1(strLine1, custcs); 

	for (boost::tokenizer<boost::char_separator<char> >::iterator it = tokcs1.begin(); it != tokcs1.end(); it++)
	{
		cout << *it << "__";
	}
	cout << endl;

	// offset_separator
	string strLine2 = "abc bcd cde def efg fgh";
	boost::replace_all(strLine2, " ", "");
	int offsets[] = {3};
	boost::offset_separator os(offsets, offsets+1);	// 每隔3个字符
	boost::tokenizer<boost::offset_separator> tokos(strLine2, os);
	for (boost::tokenizer<boost::offset_separator>::iterator it = tokos.begin(); it != tokos.end(); it++)
	{
		cout << *it << "__";
	}
	cout << endl;
}


运行效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐