boost string库的使用

没事干,发几个boost关于字符串处理的函数给大家;先来段代码:
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <string>
#include <vector>
bool foo2(const char& h)
{
	bool ret = false;
	if (h =='\\')
	{
		ret = true;
	}
	return ret;
}

void main()
{
	std::string tmp_str_test1 = "jlkjldkjslf\\\\\\\\";
	
	std::cout << boost::algorithm::trim_right_copy_if(tmp_str_test1, foo2) << std::endl;
	std::cout << tmp_str_test1 << tmp_str_test1.length() << std::endl;


	std::string tmp_str_test2 = "jljaslkdfjljf      ";

	std::cout << boost::algorithm::trim_right_copy(tmp_str_test1) << std::endl;
	std::cout << tmp_str_test2 << tmp_str_test2.length() << std::endl;

	std::cout << boost::algorithm::replace_all_copy(std::string("hello world"), "l", "-") << std::endl;

	std::string str1("abc-*-ABC-*-aBc");
	for (auto it = boost::algorithm::make_find_iterator(str1, boost::algorithm::first_finder("abc", boost::algorithm::is_iequal())); !it.eof(); ++it)
	{
		std::cout << boost::copy_range<std::string>(*it) << std::endl;
	}
	std::string str2("hello abc-*-ABC-*-aBc goodbye");
	std::vector<std::string> SplitVec;
	boost::split(SplitVec, str1, boost::is_any_of("-*"), boost::token_compress_on);
	for (std::vector<std::string>::iterator i = SplitVec.begin(); i < SplitVec.end(); i++)
	{
		std::cout << (*i) << std::endl;
	}
	//boost::algorithm::trim
	system("pause");
}


至于结果已经不再重要,大家自己没事干自己调试着玩吧,我想在大多数其他语言中这些东西都是现成的不需要自己去实现。你只要直接调用就好了,而在c++中的确缺少这些东西,所以这里不想再啰嗦介绍了无非去除左右两边的空格,去除左右两边的特定符号,替换分割等。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值