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

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;
} 


运行效果图:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目录 1 正文 3 一、 C++的string的使用 3 1.1 C++ string简介 3 1.2 string的成员 3 1.2.1 append 3 1.2.2 assign 4 1.2.3 at 4 1.2.4 begin 5 1.2.5 c_str 5 1.2.6 capacity 5 1.2.7 clear 6 1.2.8 compare 6 1.2.9 copy 6 1.2.10 _Copy_s 6 1.2.11 data 6 1.2.12 empty 6 1.2.13 end 6 1.2.14 erase 6 1.2.15 find 6 1.2.16 find_first_not_of 7 1.2.17 find_first_of 8 1.2.18 find_last_not_of 8 1.2.19 find_last_of 8 1.2.20 get_allocator 8 1.2.21 insert 8 1.2.22 length 8 1.2.23 max_size 8 1.2.24 push_back 8 1.2.25 rbegin 8 1.2.26 rend 8 1.2.27 replace 8 1.2.28 reserve 10 1.2.29 resize 11 1.2.30 rfind 11 1.2.31 size 11 1.2.32 substr 11 1.2.33 swap 11 1.3 string的构造 11 1.4 string的重载运算符 12 1.5 string与algorithm相结合的使用 12 1.5.1 string与remove 12 1.5.2 string与unique、sort 12 1.5.3 string与search 12 1.5.4 string和find、find_if 13 1.5.5 string与copy、copy_if 13 1.5.6 string与count、count_if 14 1.6 string与wstring 14 1.6.1 简介 14 1.6.2 wstring实例 15 1.6.3 wstring与控制台 15 1.6.4 string与wstring的相互转换 16 1.7 string与C++流 21 1.7.1 C++流简介 21 1.7.2 string与iostream、fstream 21 1.8 格式化字符串 22 1.8.1 简单常用的C方法 22 1.8.2 boost的方法 22 1.9 string与CString 23 二、 boost字符串算法库 23 2.1 boost字符串算法库导论 23 2.1.1 boost.algorithm.string是什么? 23 2.1.2 相关 23 2.1.3 boost.range导论 23 2.1.4 boost.regex导论 23 2.1.5 boost.algorithm.string的DNA 24 2.2 boost字符串算法解密 24 2.2.1 修剪(trim.hpp) 24 2.2.2 转换(case_conv.hpp) 26 2.2.3 判断式、断言函数(predicate.hpp)【Predicates】 27 2.2.4 查找 28 2.2.5 删除和替换 29 2.2.6 分割和组合 31 2.2.7 其它 32 三、 C字符串 32 3.1 C字符串常用算法 32 3.1.1 strcpy wcscpy 32 3.1.2 strcat wcscat 32 3.1.3 strchr wcschr 32 3.1.4 strcmp wcscmp 33 3.1.5 stricmp wcsicmp 33 3.1.6 strlen wcslen 33 3.1.7 strlwr/_strlwr wcslwr/_wcslwr 33 3.1.8 strncat wcsncat 33 3.1.9 strcspn wcscspn 33 3.1.10 strdup/_strdup wcsdup/_wcsdup 34 3.1.11 strncpy wcsncpy 34 3.1.12 strpbrk wcspbrk 35 3.1.13 strrev/_strrev wcsrev/_wcsrev 35 3.1.14 strset/_strset/_strset_l wcsset/_wcsset/_wcsset_l 35 3.1.15 strstr/wcsstr 35 3.1.16 strtok/wcstok 36 3.1.17 strupr/_strupr wcsupr/_wcsupr 36 3.2 更安全的C字符串函数 36 3.2.1 简述 36 3.2.2 简单实例 36 3.2.3 定制 38 3.2.4 兼容 41 3.3 通用字串函数 47 3.3.1 简述 47 3.3.2 简单实例 47 3.3.3 映射表 48 3.4 API级的字符串处理 48 3.4.1 简述 48 3.4.2 旧的API 48 3.4.3 Shell字符串函数 48 3.4.4 新的安全版字符串处理API 48 四、 C++字符串使用的建议 51 附录1:参考资料: 51 附录2: MSSTL中basic_string的部分源码解读 51 2.1 string的allocator 51 2.1.1 Allocate和Deallocate 51 2.1.2 allocator的泛型实现 52 2.1.3 string与char_traits 54 2.1.4 以char和wchar_t特化char_traits 56 附录3:Boost.Format中文文档 57 2.1 大纲 57 2.2 它是如何工作的 57 2.3语法 58 2.3.1 boost::format( format-string ) % arg1 % arg2 % ... % argN 58 2.3.2 printf 格式化规则 59 2.3.3 新的格式规则 60 附录4 TCHAR.h 映射表 60
目录 1 正文 4 一、 C++的string的使用 4 1.1 C++ string简介 4 1.2 string的成员 4 1.2.1 append 4 1.2.2 assign 5 1.2.3 at 5 1.2.4 begin 6 1.2.5 c_str 6 1.2.6 capacity 6 1.2.7 clear 7 1.2.8 compare 7 1.2.9 copy 7 1.2.10 _Copy_s 7 1.2.11 data 7 1.2.12 empty 7 1.2.13 end 7 1.2.14 erase 7 1.2.15 find 7 1.2.16 find_first_not_of 8 1.2.17 find_first_of 8 1.2.18 find_last_not_of 9 1.2.19 find_last_of 9 1.2.20 get_allocator 9 1.2.21 insert 9 1.2.22 length 9 1.2.23 max_size 9 1.2.24 push_back 9 1.2.25 rbegin 9 1.2.26 rend 9 1.2.27 replace 9 1.2.28 reserve 11 1.2.29 resize 12 1.2.30 rfind 12 1.2.31 size 12 1.2.32 substr 12 1.2.33 swap 12 1.3 string的构造 12 1.4 string的重载运算符 12 1.5 string与algorithm相结合的使用 13 1.5.1 string与remove 13 1.5.2 string与unique、sort 13 1.5.3 string与search 13 1.5.4 string和find、find_if 14 1.5.5 string与copy、copy_if 14 1.5.6 string与count、count_if 15 1.6 string与wstring 15 1.6.1 简介 15 1.6.2 wstring实例 15 1.6.3 wstring与控制台 16 1.6.4 string与wstring的相互转换 17 1.7 string与C++流 22 1.7.1 C++流简介 22 1.7.2 string与iostream、fstream 22 1.8 格式化字符串 23 1.8.1 简单常用的C方法 23 1.8.2 boost的方法 23 1.8.3 stlsoft + fastformat 23 1.9 string与CString 24 二、 boost字符串算法库 24 2.1 boost字符串算法库导论 24 2.1.1 boost.algorithm.string是什么? 24 2.1.2 相关 24 2.1.3 boost.range导论 24 2.1.4 boost.regex导论 24 2.1.5 boost.algorithm.string的DNA 24 2.2 boost字符串算法解密 24 2.2.1 修剪(trim.hpp) 24 2.2.2 转换(case_conv.hpp) 24 2.2.3 判断式、断言函数(predicate.hpp)【Predicates】 24 2.2.4 查找 24 2.2.5 删除和替换 24 2.2.6 分割和组合 24 2.2.7 分词 24 2.2.8 其它 24 三、 C字符串 24 3.1 C字符串常用算法 24 3.1.1 strcpy wcscpy 24 3.1.2 strcat wcscat 24 3.1.3 strchr wcschr 24 3.1.4 strcmp wcscmp 24 3.1.5 stricmp wcsicmp 24 3.1.6 strlen wcslen 24 3.1.7 strlwr/_strlwr wcslwr/_wcslwr 24 3.1.8 strncat wcsncat 24 3.1.9 strcspn wcscspn 24 3.1.10 strdup/_strdup wcsdup/_wcsdup 24 3.1.11 strncpy wcsncpy 24 3.1.12 strpbrk wcspbrk 24 3.1.13 strrev/_strrev wcsrev/_wcsrev 24 3.1.14 strset/_strset/_strset_l wcsset/_wcsset/_wcsset_l 24 3.1.15 strstr/wcsstr 24 3.1.16 strtok/wcstok 24 3.1.17 strupr/_strupr wcsupr/_wcsup

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值