Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

c++ - How to use wstring find/replace for a case insensitve replacement?

This code works perfect for string replacement, but it is case sensitive, which is what I need it to be:

WCHAR *StringReplaceEx(CONST WCHAR *orig, CONST WCHAR * pattern, CONST WCHAR  *repl, WCHAR *sOut)
{
    wstring wsoriginal = orig;
    wstring wspatterntofind = pattern;
    wstring wsreplacement = repl;
    wstring wsOut = string_replace(wsoriginal, wspatterntofind, wsreplacement);

    StringCchCopy(sOut, wcslen(wsOut.c_str())+1, wsOut.c_str());

    return sOut;
}

wstring string_replace( wstring src, wstring const& target, wstring const& repl)
{
    if ((target.length() == 0) || (src.length() == 0))
        return src;
    size_t idx = 0;
    for (;;) 
    {
        idx = src.find( target, idx);
        if (idx == wstring::npos)  
            break;

        src.replace( idx, target.length(), repl);
        idx += repl.length();
    }
    return src;
}

This works excellently, but only if the string cases match.

Is there a way to do a case-insensitive replacement?

question from:https://stackoverflow.com/questions/65919899/how-to-use-wstring-find-replace-for-a-case-insensitve-replacement

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

By using boost, even though it is outside the scope of the question, fixed the issue for me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...