按照指定字符分割字符串,并返回结果

1:编译环境

[test]$ g++ --version
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[test]$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.3 (Maipo)

 2:示例代码

/* 文件名: getSubStr.cc
 * 功能说明:按照指定字符分割字符串,并返回结果
 * g++ -o getSubStr getSubStr.cc
 * gcc -o getSubStr getSubStr.cc
 编译环境
[test]$ g++ --version
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[test]$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.3 (Maipo)
 *
 */
#include <cstdio>
#include <stdlib.h>
#include <string.h>

/* 定义log输出宏 */
#define DEB_LOG(fmat,...)  printf("%s:%d_%s()]"fmat"\n", \
    __FILE__, __LINE__, __FUNCTION__,##__VA_ARGS__)

/* 将字符串以按照指定字符分割,并返回结果 */
/* srcStr   :输入参数,源字符串
 * splitChar:输入参数,分割字符
 * columnLen:输出参数,各子字符串的长度
 * columnStr:输入参数,各子字符串
 * columnNum:输入出参数,输入最大子字符串的最大个数,输出子字符串的实际个数
 */
int getSubStr(char* srcStr, char splitChar, int columnLen[],\
        char columnStr[][50],
        int* columnNum);

int main(int argc, char* argv[]) {
    char srcStr[1024] = ",123,456,789,ab,cd,ef,g,";
    int columnLen[20];
    char columnStr[20][50];
    memset(columnStr, 0x00, sizeof(columnStr));
    int columnNum = sizeof(columnLen)/sizeof(int);
    int ret = getSubStr(srcStr, ',', columnLen, columnStr, &columnNum);
    if(0 != ret){
        DEB_LOG("%s NG", "getSubStr");
        return -1;
    }

    /* 显示子字符串 */
    DEB_LOG("%s", "start show result");
    for (int i = 0; i < columnNum; ++i) {
        DEB_LOG("strVal[%d] = %s", i, columnStr[i]);
    }
    return 0;
}

int getSubStr(char* srcStr, char splitChar, int columnLen[],\
        char columnStr[][50],
        int* columnNum) {
    if (NULL == srcStr || NULL == columnStr || NULL == columnNum) {
        return -1;
    }
    DEB_LOG("columnStr[%d] = %d", *columnNum-1, sizeof(columnStr[*columnNum-1]));

    // get value  split by ','
    char* pStart = srcStr;
    char* pEnd = NULL;
    int i = 0;
    char* bufEnd = pStart + strlen(srcStr);
    while (pStart < bufEnd) {
        pEnd = strchr(pStart, splitChar);
        if (NULL == pEnd) { // last char is not ,
            pEnd = bufEnd;
        }
        if (i < *columnNum) {
            columnLen[i] = pEnd - pStart;
            memcpy(columnStr[i], pStart, columnLen[i]);
        } else {
            DEB_LOG("subStr num > max Num[%d]", *columnNum);
            return -1;
        }
        pStart = pEnd + 1;
        ++i;
    }

    /* 判断最后一个字符是否为分割字符 */
    if (splitChar == *(bufEnd - 1)) {
        if (i < *columnNum) {
            columnLen[i] = 0;
            columnStr[i++][0] = '\0';
        } else {
            DEB_LOG("subStr num > max Num[%d]", *columnNum);
            return -1;
        }
    }
    *columnNum = i;
    return 0;
}

 

 3:输出结果

test.cc:61_getSubStr()]columnStr[8] = 50
test.cc:48_main()]start show result
test.cc:50_main()]strVal[0] = 
test.cc:50_main()]strVal[1] = 123
test.cc:50_main()]strVal[2] = 456
test.cc:50_main()]strVal[3] = 789
test.cc:50_main()]strVal[4] = ab
test.cc:50_main()]strVal[5] = cd
test.cc:50_main()]strVal[6] = ef
test.cc:50_main()]strVal[7] = g
test.cc:50_main()]strVal[8] = 

 

posted @ 2018-08-01 11:31  Chris83  阅读(967)  评论(0编辑  收藏  举报