• 实战c++中的string系列--CDuiString和string的转换(duilib中的cduistring)


    使用所duilib的人定会知道cduistring类型,先看看这个类是怎么定义的:

    class UILIB_API CDuiString
        {
        public:
            enum { MAX_LOCAL_STRING_LEN = 127/*63*/ };
    
            CDuiString();
            CDuiString(const TCHAR ch);
            CDuiString(const CDuiString& src);
            CDuiString(LPCTSTR lpsz, int nLen = -1);
            ~CDuiString();
    
            void Empty();
            int GetLength() const;
            bool IsEmpty() const;
            TCHAR GetAt(int nIndex) const;
            void Append(LPCTSTR pstr);
            void Assign(LPCTSTR pstr, int nLength = -1);
            LPCTSTR GetData() const;
    
            void SetAt(int nIndex, TCHAR ch);
            operator LPCTSTR() const;
    
            TCHAR operator[] (int nIndex) const;
            const CDuiString& operator=(const CDuiString& src);
            const CDuiString& operator=(const TCHAR ch);
            const CDuiString& operator=(LPCTSTR pstr);
    #ifdef _UNICODE
            const CDuiString& CDuiString::operator=(LPCSTR lpStr);
            const CDuiString& CDuiString::operator+=(LPCSTR lpStr);
    #else
            const CDuiString& CDuiString::operator=(LPCWSTR lpwStr);
            const CDuiString& CDuiString::operator+=(LPCWSTR lpwStr);
    #endif
            CDuiString operator+(const CDuiString& src) const;
            CDuiString operator+(LPCTSTR pstr) const;
            const CDuiString& operator+=(const CDuiString& src);
            const CDuiString& operator+=(LPCTSTR pstr);
            const CDuiString& operator+=(const TCHAR ch);
    
            bool operator == (LPCTSTR str) const;
            bool operator != (LPCTSTR str) const;
            bool operator <= (LPCTSTR str) const;
            bool operator <  (LPCTSTR str) const;
            bool operator >= (LPCTSTR str) const;
            bool operator >  (LPCTSTR str) const;
    
            int Compare(LPCTSTR pstr) const;
            int CompareNoCase(LPCTSTR pstr) const;
    
            void MakeUpper();
            void MakeLower();
    
            CDuiString Left(int nLength) const;
            CDuiString Mid(int iPos, int nLength = -1) const;
            CDuiString Right(int nLength) const;
    
            int Find(TCHAR ch, int iPos = 0) const;
            int Find(LPCTSTR pstr, int iPos = 0) const;
            int ReverseFind(TCHAR ch) const;
            int Replace(LPCTSTR pstrFrom, LPCTSTR pstrTo);
    
            int __cdecl Format(LPCTSTR pstrFormat, ...);
            int __cdecl SmallFormat(LPCTSTR pstrFormat, ...);
    
        protected:
            LPTSTR m_pstr;
            TCHAR m_szBuffer[MAX_LOCAL_STRING_LEN + 1];
        };

    以下用法:

    1 Append(LPCTSTR str) 在原字符串基础上追加一个字符串;   
    CDuiString  dui_str;   
    dui_str.Append(_T("我是中国人。"));   
    dui_str.Append(_T("我爱中国!"));     
    
    2 Assign(LPCSTR pstr ,int nLength ) 在pstr字符串的nLength位置后的东西所有剪切掉不要。   config.Assign(config, config.GetLength()-1);   
    
    3 Format(LPCSTR pstrformat, ...); 依照pstrformat字符串的格式,导入其它类型变量    
    CDuiString folde_path;   folde_path.Format(_T("%ls"), std_string); 
    
    4 GetLength()採集一个变量的宽度(大小);   
    config.GetLength();

    非常多时候 难免用到CDuiString和string的转换。

    我们应该注意到,CDuiString类有个方法:

    LPCTSTR GetData() const;

    能够通过这种方法。把CDuiString变为LPCTSTR ;
    所以下一步仅仅是怎样把LPCTSTR 转为string了。

    首先写一个StringFromLPCTSTR函数,完毕转换:

    std::string StringFromLPCTSTR(LPCTSTR str) {
    #ifdef _UNICODE
        int size_str = WideCharToMultiByte(CP_UTF8, 0, str, -1, 0, 0, NULL, NULL);
    
        char* point_new_array = new char[size_str];
    
        WideCharToMultiByte(CP_UTF8, 0, str, -1, point_new_array, size_str, NULL, NULL);
    
        std::string return_string(point_new_array);
        delete[] point_new_array;
        point_new_array = NULL;
        return return_string;
    #else
        return std::string(str);
    #endif
    }

    以下就能够完毕duicstring到string的转换了:

    CDuiString download_link = msg.pSender->GetUserData();
    std::string download_link_str = StringFromLPCTSTR(download_link.GetData());
  • 相关阅读:
    三层框架(原始版)
    Java虚拟机之内存区域
    JDK和JRE的区别
    cookie和session区别与联系
    DAO、Service、Controller及View层级结构梳理
    JavaWeb-四大域对象复习
    Mybatis-实现逆向代理
    Springboot-实现热部署
    排序算法-冒泡排序
    【ERROR 1064 (42000)】MySQL中使用mysqladmin或set修改root密码时提示语法错误
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/7100521.html
Copyright © 2020-2023  润新知