使用所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());

最新文章

  1. POI导入导出
  2. IOS开发基础知识--碎片43
  3. 海思H264解码库 hi_h264dec_w.dll 水印问题
  4. Angular 2 + Electron 开发web和桌面应用
  5. iOS/Android网络消息推送的实现两种方法
  6. 重拾Ajax
  7. Why Apache Spark is a Crossover Hit for Data Scientists [FWD]
  8. IOS开发之功能模块--自定义导航控制器类常用自定义的代码
  9. 如果使用得当,MySQL 也可以化身 NoSQL
  10. Handler笔记
  11. mysql中使用concat例子
  12. hadoop容灾能力测试
  13. EBS R12 修改 apps 密码[Z]
  14. Aras 发布Web Services
  15. Java对象序列化和返序列化
  16. redis集群搭建教程(以3.2.2为例)
  17. linux下时间有关的函数和结构体
  18. poj 3311(状态压缩DP)
  19. ideal快捷键
  20. 【Python】Python对象类型及其运算

热门文章

  1. netty百万连接跟踪记录
  2. WebService常用技术及术语
  3. Djnago进阶
  4. Coursera公开课-Machine_learing:编程作业6
  5. 【Leetcode】376. Wiggle Subsequence
  6. 5.29clone项目地址
  7. Tomcat 程序无问题的情况下页面打开变慢的原因
  8. HTTPS 为什么更安全,先看这些
  9. 富文本wangEditor的批量激活
  10. 《java数据结构与算法》系列之“数组&quot;