boost之string_algo

string_algo是用于处理字符串查找,替换,转换等一系列的字符串算法

前缀i:表示大小写不敏感

后缀_copy:表示不变动输入,返回处理结果的拷贝

后缀_if:表示算法需要一个判断式的谓词函数对象。

#include <iostream>

#include <vector>

#include <string>

#include <boost/algorithm/string.hpp>

using namespace std;

using namespace boost;



int main()

{

	string str("readme.txt");

	if (ends_with(str,"txt"))//判断后缀

	{

		cout << to_upper_copy(str) + " UPPER" <<endl;//大写

	}

	replace_first(str,"readme","followme");//替换

	cout << str <<endl;

	vector<char> v(str.begin(),str.end());

	vector<char> v2 = to_upper_copy(erase_first_copy(v,"txt"));

	for (int i = 0;i  < v2.size();++i)

	{

		cout << v2[i];

	}

	return 0;

}

1.大小写转换,如上。

2.判断式

2.1判断式(算法),用于子串的匹配

starts_with

ends_with

contains

equals

lexicographcical_compare

#include <iostream>

#include <vector>

#include <string>

#include <boost/algorithm/string.hpp>

using namespace std;

using namespace boost;



int main()

{

	string str("Power Bomb");

	if (iends_with(str,"bomb"))

	{

		cout << "success iends_with" <<endl;

	}

	if (ends_with(str,"bomb"))

	{

		cout << "success ends_with" <<endl;

	}

	if (contains(str,"er"))

	{

		cout << "success contains" <<endl;

	}

	

	return 0;

}

 2.2.判断式(函数对象)用于字符串之间的比较

#include <iostream>

#include <vector>

#include <string>

#include <boost/algorithm/string.hpp>

using namespace std;

using namespace boost;



int main()

{

	string str1("Samus");

	string str2("samus");

	if (is_equal()(str1,str2))

	{

		cout << "is equal" <<endl;

	}

	if (is_less()(str1,str2))

	{

		cout << "is less" <<endl;

	}

	

	return 0;

}

 2.3.分类(返回函数对象)用于分类

is_space判断字符是否为空格

is_alnum判断是否为字符或者数字

3.修剪

trim_left,trim_right,trim删除左右两端的空格,使用谓词判断if

#include <iostream>

#include <vector>

#include <string>

#include <boost/format.hpp>

#include <boost/algorithm/string.hpp>

using namespace std;

using namespace boost;



int main()

{

	format fmt("%s\n");

	string str = " samus aran";

	cout << fmt % trim_copy(str);

	cout << fmt % trim_left_copy(str);

	trim_right(str);

	cout << fmt % str;



	string str2 = "2013 Happy lin Year!";

	cout << fmt % trim_left_copy_if(str2,is_digit());

	cout << fmt % trim_right_copy_if(str2,is_punct());



	return 0;

}

4. 查找

find_first 查找字符串第一次出现的位置

find_last 查找字符串最后一次出现的位置

#include <iostream>

#include <vector>

#include <string>

#include <boost/format.hpp>

#include <boost/algorithm/string.hpp>

using namespace std;

using namespace boost;



int main()

{

	format fmt("%s\n.pos=%d\n");

	string str = "Long long ago,there was a king.";

	iterator_range<string::iterator> rge;

	rge = find_first(str,"long");

	cout << fmt %  rge % (rge.begin()-str.begin());

	



	return 0;

}

5.替换和删除与查找类似

#include <iostream>

#include <vector>

#include <string>

#include <boost/format.hpp>

#include <boost/algorithm/string.hpp>

using namespace std;

using namespace boost;



int main()

{

	string str = "Samus beat the monster.\n";

	cout << replace_first_copy(str,"Samus","samus");

	replace_first(str,"beat","kill");

	cout <<str;



	return 0;

}

6.分割

#include <iostream>

#include <vector>

#include <string>

#include <deque>

#include <list>

#include <boost/format.hpp>

#include <boost/algorithm/string.hpp>

using namespace std;

using namespace boost;



int main()

{

	string str = "Smaus,Link.Zelda::Mario_Luigi+zelda";

	deque<string> d;

	ifind_all(d,str,"zELDA");

	if (d.size())

	{

		cout << "split right" <<endl;

	}

	list<string> l;

	split(l,str,is_any_of(",.:_+"));

	for (list<string>::iterator it = l.begin();it != l.end();++it)

	{

		cout << *it <<endl;

	}



	return 0;

}

 7.合并

#include <iostream>

#include <vector>

#include <string>



#include <boost/assign.hpp>

#include <boost/algorithm/string.hpp>

using namespace std;

using namespace boost;

using namespace boost::assign;



int main()

{

	vector<string> v = list_of("liuwei")("linlin")("ceshi");

	cout << join(v,"+")<<endl;

	struct is_contains_i

	{

		bool operator()(const string &x)

		{

			return contains(x,"i");

		}

	};

	cout << join_if(v,"****",is_contains_i());

	return 0;

}

 

你可能感兴趣的:(String)