string 查找替换函数

该C++函数find_replace_str用于查找并替换给定字符串中的指定子串。它迭代输入字符串,找到匹配的目标子串并用新的字符串替换,同时记录替换次数。例如,将string中的tho替换为though,返回结果为thoughthoughhelloworldthough。
摘要由CSDN通过智能技术生成
std::string find_replace_str(std::string s,const std::string &oldVal,const std::string &newVal)
{
    /*****************************************************************************************
     Function:  find_replace_str
     Description:   Finding old string value in giving string s and replace it by new string.
     Input: String s is the original string going to be replaced.
            String oldVal is the replace target.
            String newVal is the final string which replaced the old value.
            If string s is "tho tho hello world tho", oldVal is "tho" and newVal is "though",
            find_replace_str(s, oldVal, newVal) return "though though hello world though" to
            be the return value.
      Author: TianYi
      Date:2023-03-10
     ****************************************************************************************/
    std::string::iterator it_n = s.begin();
    auto oldRange = static_cast<std::string::difference_type>(oldVal.size());
    int  replace_time = 0; //record the replaced time
    while (it_n != s.end()){
        if (*it_n == oldVal[0]){
            std::string::size_type oldSize = oldVal.size();
            std::string::size_type counter = 0;
            for (decltype(oldSize) i = 0; i < oldSize; ++i){
                if (*(it_n + static_cast<std::string::difference_type>(i)) == oldVal[i])
                    ++counter;
            }
            // find and insert
            if (counter == oldSize){
                auto it_range_end = it_n + oldRange;
                it_n = s.erase(it_n, it_range_end); // delete old char
                //insert new string
                auto it_new_re = newVal.crbegin();
                while (it_new_re != newVal.crend()){
                    it_n = s.insert(it_n, *it_new_re);
                    ++it_new_re;
                }
                //iterator move
                it_n += oldRange;
                ++replace_time;
                std::cout << "Replace operator has completed " << replace_time << " times.\n";
            }

        } else
            ++it_n; // iterator move
    }
    return s;
}
#include <string>
#include <list>
#include <iostream>

std::string find_replace_str(std::string s,const std::string &oldVal,const std::string &newVal)
{
    /*****************************************************************************************
     Function:  find_replace_str
     Description:   Finding old string value in giving string s and replace it by new string.
     Input: String s is the original string going to be replaced.
            String oldVal is the replace target.
            String newVal is the final string which replaced the old value.
            If string s is "tho tho hello world tho", oldVal is "tho" and newVal is "though",
            find_replace_str(s, oldVal, newVal) return "though though hello world though" to
            be the return value.
      Author: TianYi
      Date:2023-03-10
     ****************************************************************************************/
    std::string::iterator it_n = s.begin();
    auto oldRange = static_cast<std::string::difference_type>(oldVal.size());
    int  replace_time = 0; //record the replaced time
    while (it_n != s.end()){
        if (*it_n == oldVal[0]){
            std::string::size_type oldSize = oldVal.size();
            std::string::size_type counter = 0;
            for (decltype(oldSize) i = 0; i < oldSize; ++i){
                if (*(it_n + static_cast<std::string::difference_type>(i)) == oldVal[i])
                    ++counter;
            }
            // find and insert
            if (counter == oldSize){
                auto it_range_end = it_n + oldRange;
                it_n = s.erase(it_n, it_range_end); // delete old char
                //insert new string
                auto it_new_re = newVal.crbegin();
                while (it_new_re != newVal.crend()){
                    it_n = s.insert(it_n, *it_new_re);
                    ++it_new_re;
                }
                //iterator move
                it_n += oldRange;
                ++replace_time;
                std::cout << "Replace operator has completed " << replace_time << " times.\n";
            }

        } else
            ++it_n; // iterator move
    }
    return s;
}
//test
int main()
{
    std::string test = "tho tho hello world tho";
    std::string re = find_replace_str(test, "tho", "though");

    std::cout << re;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值