1、分割字符串(strsplit)
函数声明:
参数:
str为待分割的字符串
delim为分隔符的集合,注意:对"abcdeafghbi",如果delim是"abd",则结果是"c","e","fgh","i"
返回值:
被分割后的子字符串向量
定义:
函数声明:
vector
<
string
>
strsplit(
const string
str,
const
string
delim );
参数:
str为待分割的字符串
delim为分隔符的集合,注意:对"abcdeafghbi",如果delim是"abd",则结果是"c","e","fgh","i"
返回值:
被分割后的子字符串向量
定义:
vector
<
string
>
strsplit(
const
string
str,
const
string
delim )
{
int cutAt;
string lstr = str;
vector < string > result;
while ( (cutAt = lstr.find_first_of(delim)) != lstr.npos )
{
if (cutAt > 0 )
{
result.push_back(lstr.substr( 0 ,cutAt));
}
lstr = lstr.substr(cutAt + 1 );
}
if (lstr.length() > 0 )
{
result.push_back(lstr);
}
return result;
}
{
int cutAt;
string lstr = str;
vector < string > result;
while ( (cutAt = lstr.find_first_of(delim)) != lstr.npos )
{
if (cutAt > 0 )
{
result.push_back(lstr.substr( 0 ,cutAt));
}
lstr = lstr.substr(cutAt + 1 );
}
if (lstr.length() > 0 )
{
result.push_back(lstr);
}
return result;
}