3 回答
TA贡献1780条经验 获得超5个赞
直接用简单的C++
#include <iostream> #include <string> #include <vector> using namespace std; //把字符串s按照字符串c进行切分得到vector_v vector<string> split( const string& s, const string& c){ vector<string> v; int pos1=0,pos2; while ((pos2=s.find(c,pos1))!=-1){ v.push_back(s.substr(pos1, pos2-pos1)); pos1 = pos2 + c.size(); } if (pos1 != s.length()) v.push_back(s.substr(pos1)); return v; } int main() { string input= "张三$|男$|济南$|大专学历$|" ; vector<string> myArray=split(input, "$|" ); for ( int i=0;i<myArray.size();i++){ cout<<myArray[i]<<endl; } } /* 张三 男 济南 大专学历 */ |
TA贡献1712条经验 获得超3个赞
用strtok函数实现吧。
void split( char **arr, char *str, const char *del)//字符分割函数的简单定义和实现
{
char *s =NULL;
s=strtok(str,del);
while(s != NULL)
{
*arr++ = s;
s = strtok(NULL,del);
}
}
int main()
{
int i;
char *myArray[4];
char s[] = "张三$|男$|济南$|大专学历$|";
memset(myArray, 0x0, sizeof(myArray));
split(myArray, s, "$|");
for (i=0; i<4; i++)
{
printf("%s\n", myArray[i]);
}
return 0;
}
TA贡献1810条经验 获得超4个赞
char str[] = "now $| is the time for all $| good men to come to the $| aid of their country";
char delims[] = "$|";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL )
{
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}
以上代码的运行结果是:
result is "now "
result is " is the time for all "
result is " good men to come to the "
result is " aid of their country"
- 3 回答
- 0 关注
- 157 浏览
添加回答
举报