std::string 替换指定字符串中指定的子串

本文详细介绍了一种在C++中实现字符串替换和删除的方法,包括如何使用std::string进行子串查找、替换和删除操作。通过具体示例,展示了如何从字符串中删除特定子串的所有出现,以及如何用新子串替换旧子串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//************************************
// Method:        ReplaceStringInStd
// Describe:        使用指定子串替换字符串中的字符串,如果要替换的字符串为空,则移除原序列中的指定子串
// FullName:      ReplaceStringInStd
// Access:          public 
// Returns:        std::string
// Returns Describe:    返回替换操作后的字符串副本
// Parameter:    std::string strOrigin      ;原字符串
// Parameter:    std::string strToReplace   ;需要替换的字符换
// Parameter:    std::string strNewChar     ;替换后的字符换,当该字符串为空时,该函数的功能变成
//************************************
std::string ReplaceStringInStd(std::string strOrigin, std::string strToReplace, std::string strNewChar)
{
    std::string strFinal = strOrigin;
    if (strFinal.empty())
    {
        return strFinal;
    }

    if (strNewChar.empty())
    {
        size_t pos = std::string::npos;

        // Search for the substring in string in a loop untill nothing is found
        while ((pos = strFinal.find(strToReplace)) != std::string::npos)
        {
            // If found then erase it from string
            strFinal.erase(pos, strToReplace.length());
        }
    }
    else
    {
        for (std::string::size_type pos(0); pos != std::string::npos; pos += strNewChar.length())
        {
            pos = strFinal.find(strToReplace, pos);
            if (pos != std::string::npos)
                strFinal.replace(pos, strToReplace.length(), strNewChar);
            else
                break;
        }
    }
    return   strFinal;
}

 

#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
/*
* Erase First Occurrence of given  substring from main string.
*/
void eraseSubStr(std::string &mainStr, const std::string &toErase)
{
    // Search for the substring in string
    size_t pos = mainStr.find(toErase);
    if (pos != std::string::npos)
    {
        // If found then erase it from string
        mainStr.erase(pos, toErase.length());
    }
}
/*
* Erase all Occurrences of given substring from main string.
*/
void eraseAllSubStr(std::string &mainStr, const std::string &toErase)
{
    size_t pos = std::string::npos;
    // Search for the substring in string in a loop untill nothing is found
    while ((pos  = mainStr.find(toErase) ) != std::string::npos)
    {
        // If found then erase it from string
        mainStr.erase(pos, toErase.length());
    }
}
/*
* Erase all Occurrences of all given substrings from main string using C++11 stuff
*/
void eraseSubStrings(std::string &mainStr, const std::vector<std::string> &strList)
{
    // Iterate over the given list of substrings. For each substring call eraseAllSubStr() to
    // remove its all occurrences from main string.
    std::for_each(strList.begin(), strList.end(), std::bind(eraseAllSubStr, std::ref(mainStr), std::placeholders::_1));
}
/*
* Erase all Occurrences of all given substrings from main string using Pre C++11 stuff
*/
void eraseSubStringsPre(std::string &mainStr, const std::vector<std::string> &strList)
{
    // Iterate over the given list of substrings. For each substring call eraseAllSubStr() to
    // remove its all occurrences from main string.
    for (std::vector<std::string>::const_iterator it = strList.begin(); it != strList.end(); it++)
    {
        eraseAllSubStr(mainStr, *it);
    }
}
int main()
{
    std::string str = "Hi this is a sample string is for is testing is.";
    eraseAllSubStr(str, "this");
    std::cout << str << std::endl;
    eraseSubStringsPre(str, { "for", "is", "testing" });
    std::cout << str << std::endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值