本文主要涉及boost中algorithm之string问题
基本的字符串函数类型有
替换
裁剪
大小写替换
正则表达式
切割
判断
和擦除操作等等
//! boost之2:字符串算法类
#include <string>
#include <vector>
#include <iostream>
#include <iterator>
#include <functional>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
int main()
{
//! 字符串
string str(" abc-*-ABC-*-aBc ");
//! 以-或者*切割字符串
vector<std::string> ret;
split(ret,str,is_any_of("-*"),token_compress_on);
for(unsigned int index=0;index<ret.size();index++)
{
cout<<index<<":"<<ret[index]<<endl;
};
//! 切换为小写
to_lower(str);
cout<<"*"<<str<<"*"<<endl;
//! 去掉左边的空格
trim_left(str);
cout<<"*"<<str<<"*"<<endl;
//! 去掉右边的空格
trim_right(str);
cout<<"*"<<str<<"*"<<endl;
//! 替换
replace_all(str,"a","A");
cout<<str<<endl;
//! 擦除
cout<<erase_all_copy(str,"A")<<endl;
replace_nth(str,"c",2,"ccsdu2004");
cout<<str<<endl;
//! 擦除给定范围
cout<<erase_range_copy(str,make_iterator_range(str.begin()+2,str.begin()+5))<<endl;
cout<<"is start with:A:"<<starts_with(str,string("A"))<<endl;
cout<<"is end with:C:"<<ends_with(str,string("C"))<<endl;
cout<<"is contain with:ccs:"<<contains(str,string("ccs"))<<endl;
cout<<endl;
system("PAUSE");
return 0;
}