C++ replaces all strings within the specified string range in the string

overview

Replaces all character strings within the range of the specified character string with the specified character string. Such as:
source string:

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

Converted string:

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

or converted to:

“dhussd2434xhuhu是别人十大海归。”

This article is to record a method to successfully replace the above string. Among them, the first string after conversion is actually to replace the original string “$START$”with (empty string); the second is to replace the string within the original string with .“$END$”“”“$START$”“$END$”“”

implement the code

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);
}

Parameter explanation

const std::string &startStr //The start position of the replacement string, in this example is “$START$”
const std::string &endStr //The end position of the replacement string, in this example is “$END$”
const std::string &replacedStr //Replaced with the replacedStr character string, in this example it is “”
std::string &info //source string
int start //the position to start searching, in this example it is 0

function call

call one

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);

result of the call

After the call, the result of the string strTempMsg is:
dhussd2434xhuhu is someone else's top ten returnees.

call two

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

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

call result

After the call ends, the result of the string strSourceMsg is:
dfh When the flowers bloom and the moon is full, the two prime ministers are connected with each other. dhussd2434xhuhu is someone else's top ten returnees.

Notice

In the above call example, if you want the source string to remain unchanged after the string is replaced, you need to temporarily store the source string before the replacement, and assign it to the source string variable again after the replacement.

call three

Place the source string "dhus Hudhan Fans, START STARTWhen the flowers bloom and the moon is full, the two prime ministers care about each other. END ENDEN D dhussd2434xhuhu is someone else's top ten returnees. "Changed to" dhus Hu Dan Fansa place. dhussd2434xhuhu is someone else's top ten returnees. "
For the above requirements, it is necessary to replace the commas added in the above replacement function with period codes. The changed replacement function is as follows:

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);
}

The part changed by the above replacement function actually realizes that the comma before the identifier to be replaced will be changed to a period. Added the judgment whether it is Chinese or English comma.
After the above changes, the replacement function replaceDynamicString is called as follows:

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);

call result

After the call is over, the result of the string strTempMsg is:
dhus Hu Dan Fansa place, dhussd2434xhuhu is someone else's top ten returnees.

Notice

If the string can also be translated, that is, the multi-language of qt is introduced, then for the replacement of punctuation marks in this multi-language string, you have to use the idea of ​​​​multi-language to judge, and you cannot directly use the offset. (That is, the next position of the punctuation mark -1 idea), the Chinese character in the Chinese string is not one.

Guess you like

Origin blog.csdn.net/blqzj214817/article/details/130982454