3 回答

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超5個(gè)贊
直接用簡(jiǎn)單的C++
#include <iostream> #include <string> #include <vector> using namespace std; //把字符串s按照字符串c進(jìn)行切分得到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= "張三$|男$|濟(jì)南$|大專學(xué)歷$|" ; vector<string> myArray=split(input, "$|" ); for ( int i=0;i<myArray.size();i++){ cout<<myArray[i]<<endl; } } /* 張三 男 濟(jì)南 大專學(xué)歷 */ |

TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個(gè)贊
用strtok函數(shù)實(shí)現(xiàn)吧。
void split( char **arr, char *str, const char *del)//字符分割函數(shù)的簡(jiǎn)單定義和實(shí)現(xiàn)
{
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[] = "張三$|男$|濟(jì)南$|大專學(xué)歷$|";
memset(myArray, 0x0, sizeof(myArray));
split(myArray, s, "$|");
for (i=0; i<4; i++)
{
printf("%s\n", myArray[i]);
}
return 0;
}

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
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 );
}
以上代碼的運(yùn)行結(jié)果是:
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 關(guān)注
- 185 瀏覽
添加回答
舉報(bào)