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

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值