C++将字符串中包含指定字符串范围内的字符串全部替换

概述

将指定字符串所在的范围之内的字符串全部替换为指定的字符串。如:
源字符串:

“$START$dfh待到花开月圆时,两首相顾心相连。$END$dhussd2434xhuhu是别人十大海归。”

转换后的字符串:

“dfh待到花开月圆时,两首相顾心相连。dhussd2434xhuhu是别人十大海归。”

或者转换为:

“dhussd2434xhuhu是别人十大海归。”

本文就是记录一种将上述字符串替换成功的方法。其中转换之后的第一个字符串,实际上是将原有字符串中“$START$”“$END$”替换为“”(空字符串);第二个是将原有字符串中的“$START$”“$END$”范围内的字符串替换为“”

实现代码

void replaceDynamicString(
    const std::string &startStr, const std::string &endStr,
    const std::string &replacedStr,std::string &info, int start)
{
    
    
    int startPos = info.find(startStr, start);
    int endPos = info.find(endStr, start);

    bool toBeReplace = false;
    if (std::string::npos != startPos && std::string::npos != endPos &&
        startPos < endPos)
    {
    
    
        toBeReplace = true;
        int startStrSize = startStr.size();
        int endStrSize = endStr.size();
        std::string subKeyStr = info.substr(startPos + startStrSize,
                                            endPos - startStrSize - startPos);
        if (subKeyStr.size() != 0)
        {
    
    
            info.replace(startPos, endPos + endStrSize - startPos, replacedStr);
            start = startPos + replacedStr.size();
        }
        else
            start = endPos + endStrSize;
    }

    if (toBeReplace)
        replaceDynamicString(startStr, endStr, replacedStr, info,start);
}

参数讲解

const std::string &startStr //替换字符串的开始位置,本例中是“$START$”
const std::string &endStr //替换字符串的结束位置,本例中是“$END$”
const std::string &replacedStr //替换为replacedStr 字符串,本例中是“”
std::string &info //源字符串
int start //开始查找的位置,本例中为0

函数调用

调用一

QString strStart = "$PLAQUE_START$";
QString strEnd = "$PALQUE_END$";
QString strSourceMsg = "$START$dfh待到花开月圆时,两首相顾心相连。$END$dhussd2434xhuhu是别人十大海归。";
std::string strTempMsg = strSourceMsg.toStdString();
replaceDynamicString(strStart.toStdString(),
                         strEnd.toStdString(), "",
                         strTempMsg, 0);

调用的结果

调用结束后,字符串strTempMsg的结果为:
dhussd2434xhuhu是别人十大海归。

调用二

QString strStart = "$PLAQUE_START$";
QString strEnd = "$PALQUE_END$";
QString strSourceMsg = "$START$dfh待到花开月圆时,两首相顾心相连。$END$dhussd2434xhuhu是别人十大海归。";

strSourceMsg.replace(strStart , "").replace(strEnd , "");

调用结果

调用结束后,字符串strSourceMsg的结果为:
dfh待到花开月圆时,两首相顾心相连。dhussd2434xhuhu是别人十大海归。

注意

上述调用示例,要是想替换完字符串之后,源字符串依旧保持不变,需要将源字符串在替换之前先暂存,替换结束之后,再次赋给源字符串变量。

调用三

将源字符串"dhus胡但凡撒地方, S T A R T START STARTdfh待到花开月圆时,两首相顾心相连。 E N D END ENDdhussd2434xhuhu是别人十大海归。"改为“dhus胡但凡撒地方。dhussd2434xhuhu是别人十大海归。”
针对于上述需求,就需要将上面的替换函数中添加逗号的替换为句号的代码。改变之后的替换函数如下:

void ReportManager::replaceDynamicString(const std::string &startStr,
                                         const std::string &endStr,
                                         const std::string &replacedStr,
                                         std::string &info, int start)
{
    
    
    int startPos = info.find(startStr, start);
    int endPos = info.find(endStr, start);

    bool toBeReplace = false;
    if (std::string::npos != startPos && std::string::npos != endPos &&
        startPos < endPos)
    {
    
    
        toBeReplace = true;
        int startStrSize = startStr.size();
        int endStrSize = endStr.size();
        std::string subKeyStr = info.substr(startPos + startStrSize,
                                            endPos - startStrSize - startPos);
        if (subKeyStr.size() != 0)
        {
    
    
            info.replace(startPos, endPos + endStrSize - startPos, replacedStr);
            // after replace "$xxxx$" contain description msg,if prevoius pos is
            // ",",replace to be "."
            LOG_INFO("--->startPos:", info[startPos]);
            LOG_INFO("--->startPos + 1:", info[startPos + 1]);
            LOG_INFO("--->startPos - 2:", info[startPos - 2]);
            LOG_INFO("--->startPos-1", info[startPos - 1]);
            LOG_INFO("--->BEFORE CHARATER:", info[startPos - 1], "info:", info);
            // char c = m_translate.strCommas.toStdString();
            /* std::string str = info.substr(startPos-1,1);
            LOG_INFO("--->str:",str);*/
            if (info.rfind(",", startPos) != string::npos ||
                info.rfind(",", startPos) !=
                    string::npos)  // m_translate.strCommas.toStdString())//','
            {
    
    
                int nPos = 0;
                esApp->getAppConfig()->getIsChinese() // 判断是否为中文,esApp->getAppConfig()->getIsChinese(),这句代码是当时项目上的,不能照搬,明白大意即可
                    ? nPos = info.rfind(",", startPos)
                    : nPos = info.rfind(",", startPos);
                info.replace(nPos, startPos - nPos,
                             m_translate.strPeriod.toStdString());//这句将逗号替换为句号,因为引入了qt的多语言,m_translate.strPeriod代表句号,此句也不能照搬,理解大意即可
            }
            start = startPos + replacedStr.size();
        }
        else
            start = endPos + endStrSize;
    }

    if (toBeReplace)
        replaceDynamicString(startStr, endStr, replacedStr, info, start);
}

上述替换函数改动的部分,实际上实现的是将被开始替换标识符前的逗号,改为句号。加入了判断是中文还是英文逗号。
经过上述的改动之后,替换函数replaceDynamicString的调用如下:

QString strStart = "$PLAQUE_START$";
QString strEnd = "$PALQUE_END$";
QString strSourceMsg = "dhus胡但凡撒地方,$START$dfh待到花开月圆时,两首相顾心相连。$END$dhussd2434xhuhu是别人十大海归。";
std::string strTempMsg = strSourceMsg.toStdString();
replaceDynamicString(strStart.toStdString(),
                         strEnd.toStdString(), "",
                         strTempMsg, 0);

调用结果

调用结束后,字符串strTempMsg的结果为:
dhus胡但凡撒地方,dhussd2434xhuhu是别人十大海归。

注意

如果字符串也是可以被翻译的,也就是引入了qt的多语言,那么针对于这种多语言字符串中的标点符号替换,就得利用多语言的思想来进行判断,不能直接利用偏移,(即标点符号的下一个位置-1的思想),中文字符串中中文所占字符不是一。

猜你喜欢

转载自blog.csdn.net/blqzj214817/article/details/130982454