直接上源码:
//查找一个字符在一个字符串中第n次出现的位置
int findNstPositon(char *str,char c,int n)
{
char *p = str;
int index = 0;
int count = 0;
while(*p != '\0')
{
if(*p == c)
{
count ++;
}
if(count < n)
{
p++;
index++;
}else {
break;
}
}
return index;
}
//以指定的字符分割字符串,并将分割后的字符串组放入vector<string> 中
void split(const string& src, const string& separator, vector<string>& dest)
{
string str = src;
string substring;
string::size_type start = 0, index;
do
{
index = str.find_first_of(separator,start);
if (index != string::npos)
{
substring = str.substr(start,index-start);
dest.push_back(substring);
start = str.find_first_not_of(separator,index);
if (start == string::npos) return;
}
}while(index != string::npos);
//the last token
substring = str.substr(start);
dest.push_back(substring);
}