返回> 网站首页 

stl - trim、split

yoours2023-02-18 22:06:25 阅读 1170

简介一边听听音乐,一边写写文章。

一、头文件

#include <list>

#include <vector>

#include <algorithm>

using namespace std;


二、lTrim、rTrim、trim

string& lTrim(string &ss)

{

string::iterator p=find_if(ss.begin(),ss.end(),not1(ptr_fun(isspace)));

ss.erase(ss.begin(),p);

return ss;

}


string& rTrim(string &ss)

{

string::reverse_iterator p=find_if(ss.rbegin(),ss.rend(),not1(ptr_fun(isspace)));

ss.erase(p.base(),ss.end());

return ss;

}


string& trim(string &st)

{

lTrim(rTrim(st));

return st;

}


三、split

int split(const string str, vector<string>& ret_, string sep = ",")

{

if (str.empty())

return 0;


string tmp;

string::size_type pos_begin = str.find_first_not_of(sep);

string::size_type comma_pos = 0;


while (pos_begin != string::npos)

{

comma_pos = str.find(sep, pos_begin);

if (comma_pos != string::npos)

{

tmp = str.substr(pos_begin, comma_pos - pos_begin);

pos_begin = comma_pos + sep.length();

}else{

tmp = str.substr(pos_begin);

pos_begin = comma_pos;

}


if (!tmp.empty())

{

ret_.push_back(tmp);

tmp.clear();

}

}

return 1;

}


微信小程序扫码登陆

文章评论

1170人参与,0条评论