字符串基本处理函数

来源:互联网 发布:淘宝开抢提醒 编辑:程序博客网 时间:2024/05/21 21:50

随手写了两个字符串基本处理函数,记录下,方便以后不用写采用的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;}

0 0
原创粉丝点击