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

来源:互联网 发布:如何修改路由器mac地址 编辑:程序博客网 时间:2024/04/27 03:20

#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