如何使用 wstring 查找/替换进行不区分大小写的替换?

问题描述

这段代码非常适合字符串替换,但它区分大小写,这正是我需要的:

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

这非常有效,但前提是字符串大小写匹配。

有没有办法进行不区分大小写的替换?

解决方法

通过使用 boost,即使它超出了问题的范围,也为我解决了这个问题。