获取两个逗号之间的内容

    /*
    从字符串my_date("a1, b22, c333, d4444, e55555, f6666")中,
    获取第num到第(num+1)个逗号之间的内容,
    该内容的首地址由参数pp_begin带出,长度由参数len带出
    */
    void smn_get_date_from_douhao(char* my_date, int num, char** pp_begin, int* len)
    {
        char* tem_begin = NULL;
        char* tem_end = NULL;
        char* tem_char_post = NULL;
        //char* tem_begin, *tem_end, *tem_char_post;//多个指针的声明,要有*

        int douhao_num = 0;

        if (NULL != my_date)
        {
            tem_begin = tem_end = tem_char_post = my_date;

            while((NULL != tem_end))
            {
                tem_end = strchr(tem_char_post, ',');//返回字符串tem_char_post中首次出现字符‘,’的位置
                if(NULL == tem_end)
                {
                    return;
                }
                tem_end++;

                if (douhao_num == num)
                {
                    *len = tem_end - tem_begin - 1;
                    *pp_begin = tem_begin;

                    break;
                }
                douhao_num++;
                tem_char_post = tem_begin = tem_end;//移动tem_char_post、tem_begin到tem_end指向的位置
            }
        }
    }


    int _tmain(int argc, _TCHAR* argv[])
    {
        char date[] = "a1, b22, c333, d4444, e55555, f6666";

        char* p_begin = NULL;
        int len = 0;

        smn_get_date_from_douhao(date, 3, &p_begin, &len);

        char* p_buf = (char*)malloc((len + 1) * sizeof(char));

        strncpy(p_buf, p_begin, len);

        p_buf[len] = '\0';

        printf("%s", p_buf);

        free(p_buf);
        p_buf = NULL;

        getchar();
        return 0;
    }

猜你喜欢

转载自blog.csdn.net/lyl0530/article/details/80795074
今日推荐