C/C++语言以某符号分割字符串

实例一 

#include 
#include 
 
int main(int argc, char *argv[])
{
    char test_str[] = "this,is,a,,,test";
    char *ptr,*retptr;
    int i=0;
 
    ptr = test_str;
 
    while ((retptr=strtok(ptr, ",")) != NULL) {
        printf("substr[%d]:%s\n", i++, retptr);
        ptr = NULL;
    }
 
    return 0;
}

实例二

#include 
#include 
 
int main()
{
    char arch[6];
    char abc[] = "aaa.bbb";
    strtok(abc, ".");
    strcpy(arch, strtok(NULL, "."));
    printf ("%s %s\n", abc, arch);  

    return 0;
}


结果:aaa bbb

 

你可能感兴趣的:(C/C++)