c++string与wstring互转,Unicode与UTF-8互转,字符串替换,大小写字母转换

sting转wstring

std::wstring AnsiToUnicode(const std::string& strA)
    {
        int  unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, strA.c_str(), -1, NULL, 0);
        if (unicodeLen <= 0)
        {
            return (L"");
        }

        wchar_t* pUnicode = new wchar_t[unicodeLen + 1];
        memset(pUnicode, 0, (unicodeLen + 1)*sizeof(wchar_t));

        ::MultiByteToWideChar(CP_ACP, 0, strA.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);

        std::wstring rt = pUnicode;

        delete[] pUnicode;
        pUnicode = NULL;

        return rt;
    }

 

wstring转string

    std::string UnicodeToAnsi(const std::wstring& strW)
    {
        int iTextLen = WideCharToMultiByte(CP_ACP, 0, strW.c_str(), -1, NULL, 0, NULL, NULL);

        if (iTextLen <= 0)
        {
            return "";
        }

        char* pElementText = new char[iTextLen + 1];
        memset(pElementText, 0, sizeof(char) * (iTextLen + 1));

        ::WideCharToMultiByte(CP_ACP, 0, strW.c_str(), -1, pElementText, iTextLen, NULL, NULL);

        std::string strText = pElementText;

        delete[] pElementText;
        pElementText = NULL;

        return strText;
    }

 

UTF-8转Unicode

    //UTF-8 to Unicode
    std::wstring UTF8ToUnicode(const std::string& str)
    {
        int unicodeLen = ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
        if (unicodeLen <= 0)
        {
            return L"";
        }

        wchar_t* pUnicode = new  wchar_t[unicodeLen + 1];
        memset(pUnicode, 0, (unicodeLen + 1)*sizeof(wchar_t));

        ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);

        std::wstring  rt = pUnicode;

        delete[] pUnicode;
        pUnicode = NULL;

        return rt;
    }

 

Unicode转UTF-8

    //Unicode to UTF-8
    std::string UnicodeToUTF8(const std::wstring& str)
    {
        int iTextLen = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL);
        if (iTextLen <= 0)
        {
            return "";
        }

        char* pElementText = new char[iTextLen + 1];
        memset(pElementText, 0, sizeof(char) * (iTextLen + 1));
        ::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL);

        std::string strText = pElementText;

        delete[] pElementText;
        pElementText = NULL;

        return strText;
    }

 

字符串替换

    template<class T>
    void _StringReplace(T& strExpr, const T& strsrc, const T& strdst, size_t offset)
    {
        T::size_type pos = offset;
        T::size_type srclen = strsrc.size();
        T::size_type dstlen = strdst.size();

        while ((pos = strExpr.find(strsrc, pos)) != T::npos)
        {
            strExpr.replace(pos, srclen, strdst);
            pos += dstlen;
        }
    }

    void StringReplace(std::string& strExpr, const std::string& strsrc, const std::string& strdst, size_t offset)
    {
        _StringReplace(strExpr, strsrc, strdst, offset);
    }

    void StringReplace(std::wstring& strExpr, const std::wstring& strsrc, const std::wstring& strdst, size_t offset)
    {
        _StringReplace(strExpr, strsrc, strdst, offset);
    }

 

大小写字符转换

    std::wstring& StringToUpper(std::wstring& str)
    {
        for (auto first = str.begin(); first != str.end(); ++first)
        {
            *first = toupper(*first);
        }
        return str;
    }

 

    std::wstring& StringToLower(std::wstring& str)
    {
        for (auto first = str.begin(); first != str.end(); ++first)
        {
            *first = tolower(*first);
        }
        return str;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搁浅的渔

创作不易,多多支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值