-
翻阅古今
直接用简单的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; }}/*张三男济南大专学历*/
-
交互式爱情
用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;}
-
慕莱坞森
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"