字符串分割和替换函数(常用)

对于字符串,主要的操作时字符串替换和字符串分割,下面给出这几个函数

//简洁,不保留空串
std::vector<std::string> split_s(const char* src, const char* delim);

//保留空串,防止参数错误
std::vector<std::string> split_x(const char* src, const char* delim);
std::vector<std::string> split_c(const char* src, char delim);

//整体替换
std::string replace_all_distint(const std::string& src, const std::string& old_str, const std::string& new_str);
std::string replace_all_distint(const char* src, const char* old_s, const char* new_s);

//所有的分别替换
std::string replace_all_distint_char(const std::string& src, const std::string& old_str, char new_w);
std::string replace_all_distint_char(const char* src, const char* old_s, char new_w);
std::string replace_all_distint_char(const std::string& src, const std::string& old_str, char new_w);

上面是这些函数的声明,注意需要包含一些STL头文件 vector, string, sstream

下面的是具体实现

/*
字符串分割函数
参数:
---src: 被分割的字符串
---delim: 分隔符所组成的字符串
功能:将被分割的字符串中所有的delim换成\0,进行分割,连在一起的当成一个
例如:split_s("AAABBCCGGGBDDD", "GB") = {"AAA", "CC", "DDD"}
*/
std::vector<std::string> split_s(const char* src, const char* delim)
{
    std::vector<std::string> vec;
    if (NULL == src || NULL == delim)
    {
        return vec;
    }
    char* p;
    char* source = new char[strlen(src) + 1];
    strcpy(source, src);
    p = strtok(source, delim);
    while (p)
    {
        std::string str(p);
        p = strtok(NULL, delim);
        vec.push_back(str);
    }
    delete source;
    return vec;
}
/*
字符串分割
按照给的分隔符,彻底分解字符串,分隔符如果紧挨在一起则中间中空串表示
例如:split_x("AAABBCCGGGBDDD", "GB") = {"AAA", "", "CC", "", "", "", "DDD"}
*/
std::vector<std::string> split_x(const char* src, const char* delim)
{
    if (NULL == src || NULL == delim)
    {
        std::vector<std::string> vec;
        return vec;
    }

    std::string all = replace_all_distint_char(src, delim, delim[0]);

    return split_c(all.c_str(), delim[0]);
}

/*
按照某个字符分割字符串
split_c("AAABBCCGGGBDDD", 'B') = {"AAA", "", "CC", "GGG", "DDD"}
*/
std::vector<std::string> split_c(const char* src, char delim)
{
    std::vector<std::string> vec;
    if (NULL == src)
    {
        return vec;
    }

    stringstream is(src);

    string line;
    while(getline(is,line,delim))
    {
        vec.push_back(line);
    }
    return vec;
}

/*
将字符串替换,直到不在出现old_s,这个不常用
如 replace_all("112211", "12", "21") => 121211 => 212111 => 221111
*/
std::string replace_all(char* src, const char* old_s, const char* new_s)
{
    std::string source(src);
    std::string old_str(old_s);
    std::string new_str(new_s);

    while(true)
    {
        std::string::size_type pos(0);
        if ((pos = source.find(old_str)) != string::npos)
        {
            source.replace(pos,old_str.length(), new_str);
        }
        else
        {
            break;
        }
    }
    return source;
}

/*
将所有old_str换成new_str
*/
std::string replace_all_distint(const std::string& src, const std::string& old_str, const std::string& new_str)
{
    std::string source(src);
    for (std::string::size_type pos(0); pos != std::string::npos; pos+=new_str.length())
    {
        if ((pos = source.find(old_str, pos)) != string::npos)
        {
            source.replace(pos, old_str.length(), new_str);
        }
        else
        {
            break;
        }
    }
    return source;
}

/*
重载上面的函数
*/
std::string replace_all_distint(const char* src, const char* old_s, const char* new_s)
{
    std::string source(src);
    std::string old_str(old_s);
    std::string new_str(new_s);

    return replace_all_distint(source, old_str, new_str);
}

/*
将所有old_str中出现过的字符都替换为new_w
*/
std::string replace_all_distint_char(const std::string& src, const std::string& old_str, char new_w)
{
    std::string source(src);

    for (int i = 0; i < source.length(); i++)
    {
        if (old_str.find(source[i], 0) != std::string::npos)
        {
            source[i] = new_w;
        }
    }
    return source;
}
/*
重载上面的函数
*/
std::string replace_all_distint_char(const char* src, const char* old_s, char new_w)
{
    std::string source(src);
    std::string old_str(old_s);

    return replace_all_distint_char(source, old_str, new_w);
}
posted @ 2012-12-02 20:38  哈哈开心  阅读(1005)  评论(0编辑  收藏  举报