WEB开发网
开发学院软件开发C++ 字符串分割 阅读

字符串分割

 2012-05-17 16:23:38 来源:WEB开发网   
核心提示:#include <iostream>#include <string.h>#include <stdio.h>#include <sys/types.h>using namespace std;/* 122,11232,122342,122,332-332,212
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
using namespace std;

/*
    122,11232,122342,122,332-332,2123-332
    如何处理上面到数据呢。。。

*/

//typedef unsigned int  uint;
const char *TakeOutStringByChar(const char *src,const char ch,char *outbuff,uint len)
{
    /*
     三种情况
     *sincoder\0
     *sincoder*
     sincoder*
    */
    const char *start = src;
    uint  substrlen = 0;
    bool  isfind = false;
    while(*src)
    {
        if(*src == ch)
        {
            isfind = true;
            if(substrlen)
            {
                if(len <= substrlen)
                  return NULL;
                memset(outbuff,0,len);
                memcpy(outbuff,start,substrlen);
                return src;
            }
            else
            {
                start = ++src;
                continue;
            }
        }
        ++src;
        ++substrlen;
    }
    if(isfind && substrlen)
    {
        if(len <= substrlen)
            return NULL;
        memset(outbuff,0,len);
        memcpy(outbuff,start,substrlen);
        return src;
    }
    return NULL;
}

int main(int a,char **argv)
{
    const char *p = argv[1];
    char buff[1024];
    while((p = TakeOutStringByChar(p,',',buff,1024)))
    {
        printf("%s\n",buff);
    }
    return 0;
}

Tags:字符串 分割

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接