织梦CMS - 轻松建站从此开始!

罗索

C/C++实现split分割字符串

落鹤生 发布于 2011-06-01 14:43 点击:次 
C & C++字串处理 - 实现字串分割split
TAG:

C++实现方法:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

void split(const string& src, const string& separator, vector<string>& dest)
{
    string str = src;
    string substring;
    string::size_type start = 0, index;

    do
    {
        index = str.find_first_of(separator,start);
        if (index != string::npos)
        {   
            substring = str.substr(start,index-start);
            dest.push_back(substring);
            start = str.find_first_not_of(separator,index);
            if (start == string::npos) return;
        }
    }while(index != string::npos);
   
    //the last token
    substring = str.substr(start);
    dest.push_back(substring);
}


int main()
{
    string src = "Accsvr:tcp     -h   127.0.0.1 -p \t 20018   ";
    vector<string> d, s;
    vector<string>::iterator p, q;
   
   
    split(src,":",d);
   
    for(p=d.begin();p!=d.end();++p)
    {
        cout << *p << endl;
        s.clear();
        split(*p," \t\n",s);
        for (q=s.begin();q!=s.end();++q)
            cout << "\t" << *q << endl;
    }

    return 0;
}

C语言实现方法一:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void split(char *src, const char *separator, char **dest, int *num)
{
    char *pNext;
    int count = 0;
   
    if (src == NULL || strlen(src) == 0) return;
    if (separator == NULL || strlen(separator) == 0) return;

    pNext = strtok(src,separator);
   
    while(pNext != NULL)
    {
        *dest++ = pNext;
        ++count;
        pNext = strtok(NULL,separator);
    }

    *num = count;
}

int main()
{
    char src[] = "Accsvr:tcp  -h    127.0.0.1      -p\n    20018";
    char *dest[128];
    char *dest2[128];
    int num = 0, num2 = 0;
    int i, j;

    split(src,":",dest,&num);

    for (i=0;i<num;++i)
    {
        printf("|%s|\n",dest[i]);
        split(dest[i]," \t\n",dest2,&num2);
        for (j=0;j<num2;++j)
        {
            printf("|%s|\n",dest2[j]);
        }
    }

    return 0;
}

C语言实现方法二:功能与方法一有区别

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void split(char *src, const char *separator, char **dest, int *num)
{
    char *pSeparator, *pStart, *pEnd;
    unsigned int sep_len;
    int count = 0;
   
    if (src == NULL || strlen(src) == 0) return;
   
    pSeparator = (char *)malloc(16);
    if (pSeparator == NULL) return;
   
    if (separator == NULL || strlen(separator) == 0) strcpy(pSeparator," ");/* one blank by default */
    else strcpy(pSeparator,separator);

    sep_len = strlen(pSeparator);
       
    pStart = src;
   
    while(1)
    {
        pEnd = strstr(pStart, pSeparator);
        if (pEnd != NULL)
        {
            memset(pEnd,'\0',sep_len);
            *dest++ = pStart;
            pEnd = pEnd + sep_len;
            pStart = pEnd;
            ++count;
        }
        else
        {
            *dest = pStart;
            ++count;
            break;
        }
    }

    *num = count;

    if (pSeparator != NULL) free(pSeparator);
}

int main()
{
    char src[] = "Accsvr:tcp  -h    127.0.0.1    -p    20018";
    char *dest[128];
    char *dest2[128];
    int num = 0, num2 = 0;
    int i, j;

    split(src,":",dest,&num);

    for (i=0;i<num;++i)
    {
        printf("|%s|\n",dest[i]);
        split(dest[i],"\t",dest2,&num2);
        for (j=0;j<num2;++j)
        {
            printf("|%s|\n",dest2[j]);
        }
    }

    return 0;
}

(秩名)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201106/14519.html]
本文出处:网络博客 作者:秩名
顶一下
(4)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容