您的位置:首页 > 其它

字符串基本处理函数

2015-09-06 09:47 239 查看
随手写了两个字符串基本处理函数,记录下,方便以后不用写采用的c++ string的库函数

一个是去掉前后某个字符的函数,第二个是将字符串按照某个字符分成数组

std::string Trim(std::string str, char ch)
{
string::size_type bPos = str.find_first_not_of(ch);
string::size_type ePos = str.find_last_not_of(ch);

if(bPos == string::npos || ePos == string::npos)
return string("");

return str.substr(bPos, ePos - bPos + 1);
}

bool Split(vector<string>& strVec, string str, char ch)
{
if(str.empty())
return false;

string::size_type bpos = str.find_first_not_of(ch);
string::size_type epos = 0;

string tmp;
while(bpos != string::npos)
{
epos = str.find(ch, bpos);
if(epos != string::npos)
{
tmp = str.substr(bpos, epos - bpos);
bpos = epos + 1;
}
else
{
tmp = str.substr(bpos);
bpos = epos;
}
if(!tmp.empty())
{
strVec.push_back(tmp);
tmp.clear();
}
}
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: