65,187
社区成员




#include <string>
#include <iostream>
using namespace std;
int main()
{
string str="hello;good;word;nihao";
string str2[10];
string::size_type first=0,end=0;
int i=0;
while(1)
{
if( (end=str.find(";",first)) != string::npos)
{
str2[i++]=str.substr(first,end-first);
first=end+1;
}
else
break;
}
str2[i]=str.substr(first,str.size()-first);
return 0;
}
#include <string.h>
#include <stdio.h>
char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,;\t\n"; // 各种分隔符号都可以
char *token;
void main( void )
{
printf( "%s\n\nTokens:\n", string );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, seps );
}
}
int Split(const string &str, const string &splitchar, vector <string> &vec)
{
string stmp = "";
string::size_type pos = 0, prev_pos = 0;
int j = 0;
vec.clear();
while ((pos = s.find_first_of(splitchar, pos)) != string::npos)
{
stmp = s.substr(prev_pos, pos - prev_pos);
vec.push_back(stmp);
prev_pos = ++pos;
}
stmp = s.substr(prev_pos, pos - prev_pos);
if (stmp.length() > 0)
{
vec.push_back(stmp);
}
return 0;
}