C++分割字符串数组

#include <stdio.h>
#include <string.h>
int main(void)  
{  
        char str[] = "2015-3-22,abcd,efg,hij,klmn,opq,rst,uvw,xyz,tao";/*待分隔的原字符串*/  
        char seg[] = ","; /*分隔符这里为逗号comma,分隔符可以为你指定的,如分号,空格等*/
        char charlist[50][50]={""};/*指定分隔后子字符串存储的位置,这里定义二维字符串数组*/
        int i =0;
        char *substr= strtok(str, seg);/*利用现成的分割函数,substr为分割出来的子字符串*/
        while (substr != NULL) {  
                strcpy(charlist[i],substr);/*把新分割出来的子字符串substr拷贝到要存储的charlsit中*/
                i++;
                printf("%s\n", substr);  
                substr = strtok(NULL,seg);/*在第一次调用时,strtok()必需给予参数str字符串,
                往后的调用则将参数str设置成NULL。每次调用成功则返回被分割出片段的指针。*/
        } 
        getchar();/*press enter key to continue*/
        return 0;  
}


转载:https://blog.csdn.net/goodtomsheng/article/details/44543161

猜你喜欢

转载自blog.csdn.net/lgq2016/article/details/84936419