直接用代碼描述string str = "abc|def|gh||mno";對以上字符串使用strtok_s函數,結果為abc,def,gh,mno但自己想得到的是abc,def,gh,,mno //注意,gh后面是兩個逗號拜托了,謝謝現在是有辦法去實現,就是在調用該函數的同時去判斷剩余的字符串內容的首字符是否為標識符,但這樣就需要在每次調用strtok_s函數時去做判斷,有沒有更優的解決方案呢?求教
2 回答

婷婷同學_
TA貢獻1844條經驗 獲得超8個贊
char sentence[]="This|is|a|sentence|with|7|||tokens";
cout<<"The string to be tokenized is:\n"<<sentence<<"\n\nThe tokens are:\n\n";
char *tokenPtr=strtok(sentence,"|");
while( tokenPtr )
{
cout<<tokenPtr<<" ";
tokenPtr=strtok(NULL,"|");
}
cout<<endl;
cout<<"After strtok, sentence="<<sentence<<endl;
用strtok 就可以了
- 2 回答
- 0 關注
- 193 瀏覽
添加回答
舉報
0/150
提交
取消