<Boost> 字符串处理和格式化输出
2015-01-06 11:38
441 查看
boost有丰富的字符串处理和格式化输出库。
boost::string_algo
用于字符串大小写转换,比较,修剪和查找
boost::format
用于字符串格式化输出,如printf一般.
boost::tokenizer
用于字符串分割
下面是测试代码:
运行效果图:
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; }
运行效果图:
相关文章推荐
- 页面输出时用js转义替换字符串中的<script></script>标签,防止XSS
- <实例>输出给定字符串的全部连续子序列
- <s:if> 字符串操作
- <转> Java Socket常见异常处理
- <PY><core python programming笔记>C6 序列:字符串和列表和元组(三)
- <PY><core python programming笔记>C6 序列:字符串和列表和元组(二)
- <PY>今天研究了一下批处理,py拖动生成单文件exe
- <textarea>里的内容保存MYSQL后,页面输出不换行的解决方法
- <WEB>平板_手机开发_13 个处理触摸事件和多点触摸的JS 库
- 《C++第九周实验报告1-1》--------接第8周任务1,定义Complex类中的<<和>>运算符的重载,实现输入和输出
- 第九周【项目1】在第8周项1的基础上(1)再定义一目运算符 -,-c相当于0-c(2)定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然
- <转载>JAVA AES DES MD5 加密字符串
- <转>Android图片处理(Matrix,ColorMatrix)
- 第九周试验任务1--定义Time类中的<<和>>运算符的重载,实现输入和输出。
- <车载物联网项目,视频采集传输部分,第二天> 使用ffserver输出视频文件流,并且使用使用命令行终端播放视频文件
- C++中输入输出<< 和>>重载,以便适应输出输入一个对象
- 《C++第九周实验报告2-1》---接第8周任务2,定义Time类中的<<和>>运算符重载,实现时间的输入输出
- 在<s:iterator>中,将时间输出显示格式化
- 在ibatis动态sql中的< 、> 、<>等比较符号的处理
- struts2 <s:textfield />中的日期格式化输出