实战c++中的string系列--CDuiString和string的转换(duilib中的cduistring)
2015-12-12 15:46
1096 查看
使用所duilib的人定会知道cduistring类型,先看看这个类是怎么定义的:
下面使用方法:
很多时候 难免用到CDuiString和string的转换。
我们应该注意到,CDuiString类有个方法:
可以通过这个方法,把CDuiString变为LPCTSTR ;
所以下一步只是如何把LPCTSTR 转为string了。
首先写一个StringFromLPCTSTR函数,完成转换:
下面就可以完成duicstring到string的转换了:
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());
相关文章推荐
- 实战c++中的string系列--CDuiString和string的转换(duilib中的cduistring)
- 简单利用模板的例子
- 关于CodeBlocks中stdc++-6.dll缺失的小问题
- 关于CodeBlocks中stdc++-6.dll缺失的小问题
- C++ <chrono> 时间库
- C语言的printf函数的执行顺序
- C语言总结
- C++常用字符串分割方法
- c语言之变量存储类型
- C语言基础总结1
- C语言中的关键字
- Persistence机制
- VC++程序调试debug dump
- malloc函数的使用
- 实战c++中的string系列--std:vector<char> 和std:string相互转换(vector to stringstream)
- 实战c++中的string系列--std:vector<char> 和std:string相互转换(vector to stringstream)
- C++静态库与动态库
- 《Effective C++ 》学习笔记——规定10
- C语言总结(全部)
- Android实时获得经纬度,发送给c++服务端