2021-11-08

这里写自定义目录标题

C++分割字符串std::string

将字符串str按照分割的字符串s,进行分割,将结果以std::vectorstd::string形式返回。
如str=“110.22,120.22,130.22,140.22”
s=","
std::vectorstd::stringret=[“110.22”,“120.22”,“130.22”,“140.22”];

代码如下


#include <iostream>
#include <vector>
#include <string>

std::vector<std::string> splitString(const std::string& str, const std::string& s)
{
	std::vector<std::string> ret;
	int sLen = s.size();
	int index = 0;
	int endIndex = str.find(s, index);
	while(endIndex > 0)
	{
		if(endIndex > index)
		{
			std::string temp = str.substr(index, endIndex-index);
			ret.push_back(temp);
			index = endIndex + sLen;
		}
		endIndex = str.find(s, index);
	}
	if(index < str.size() )
	{
		std::string temp = str.substr(index, str.size() - index);
		ret.push_back(temp);
	}
	return ret;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值