字符串分割实现示例:
#include <string>
#include <vector>
#include <iostream>
using namespace std;
//字符串分割函数
vector<string> StrSplit(string strUserInfo, string strSplitPoint)
{
string::size_type strPos;
vector<string> vResult;
int nSize = strUserInfo.size();
for (int i = 0; i < nSize; i++)
{
strPos = strUserInfo.find(strSplitPoint, i);
if (strPos < nSize)
{
std::string s = strUserInfo.substr(i, strPos - i);
vResult.push_back(s);
i = strPos + strSplitPoint.size() - 1;
}
}
return vResult;
}
int main() {
string strUserInfo = "H e l l o W o r d !";
vector<string> vResult = StrSplit(strUserInfo, " ");
for (int i = 0; i < vResult.size(); i++)
{
cout << "[" << vResult[i] << "]" << endl;
}
return 0;
}
输出显示:
substr()函数:按照条件截取字符串
原型:string substr (size_t pos = 0, size_t len = npos) const;
参数说明:pos=截取起始位 ,len=截取长度。
返回值:子字符串
鼠标单击链接跳转到对应网址: substr释义.