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

实战c++中的string系列--CDuiString和string的转换(duilib中的cduistring)的更多相关文章

  1. String详解, String和CharSequence区别, StringBuilder和StringBuffer的区别 (String系列之1)

    本章主要介绍String和CharSequence的区别,以及它们的API详细使用方法. 转载请注明出处:http://www.cnblogs.com/skywang12345/p/string01. ...

  2. 《数据分析实战:基于EXCEL和SPSS系列工具的实践》一1.4 数据分析的流程

    本节书摘来华章计算机<数据分析实战:基于EXCEL和SPSS系列工具的实践>一书中的第1章 ,第1.4节,纪贺元 著 更多章节内容可以访问云栖社区"华章计算机"公众号查 ...

  3. java中 列表,集合,数组之间的转换

    java中 列表,集合,数组之间的转换 java中 列表,集合,数组之间的转换 java中 列表,集合,数组之间的转换 List和Set都是接口,它们继承Collection(集合),集合里面任何数据 ...

  4. 实战c++中的string系列--std:vector 和std:string相互转换(vector to stringstream)

    string.vector 互转 string 转 vector vector  vcBuf;string        stBuf("Hello DaMao!!!");----- ...

  5. 实战c++中的string系列--string与char*、const char *的转换(data() or c_str())

    在project中,我们也有非常多时候用到string与char*之间的转换,这里有个一我们之前提到的函数 c_str(),看看这个原型: const char *c_str(); c_str()函数 ...

  6. 实战c++中的string系列--string的替换、查找(一些与路径相关的操作)

    今天继续写一些string操作. string给我们提供了非常多的方法,可是每在使用的时候,就要费些周折. 场景1: 得到一个std::string full_path = "D:\prog ...

  7. 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)

    使用vector容器,即避免不了进行查找,所以今天就罗列一些stl的find算法应用于vector中. find() Returns an iterator to the first element ...

  8. Java基础扫盲系列(-)—— String中的format

    Java基础扫盲系列(-)-- String中的format 以前大学学习C语言时,有函数printf,能够按照格式打印输出的内容.但是工作后使用Java,也没有遇到过格式打印的需求,今天遇到项目代码 ...

  9. 实战c++中的vector系列--知道emplace_back为何优于push_back吗?

    上一篇博客说道vector中放入struct.我们先构造一个struct对象.再push_back. 那段代码中,之所以不能使用emplace_back,就是由于我们定义的struct没有显示的构造函 ...

随机推荐

  1. Win10重置 系统诸多设置或者菜单点击无效或者异常信息回复办法

    cmd: 输入下列脚本重新注册DLL文件,待执行完毕后重启电脑 for %1 in (%windir%\system32\*.dll) do regsvr32.exe /s %1

  2. mac下idea卡顿问题解决

    idea在加载相对来说比较大的系统时,经常性出现卡顿,就是直接卡死,以至于写起代码特别难受. 最后找到的解决方案是修改idea.vmoptions中的内存大小 执行 find / -name idea ...

  3. MyEclipse中快速复制粘贴当前行的操作

  4. centos下使用shell+expect远程登录主机

    # 安装expect yum install expect # 新建脚本文件running #!/usr/bin/expect spawn /usr/bin/ssh root@114.114.114. ...

  5. Laravel5.1学习笔记9 系统架构1 请求生命周期 (待修)

    Request Lifecycle Introduction Lifecycle Overview Focus On Service Providers Introduction When using ...

  6. Challenge–response authentication 挑战(询问)应答机制

    In computer security, challenge–response authentication is a family of protocols in which one party ...

  7. vue-router在同一个路由下切换,取不到变化的路由参数

    最近用vue写项目的时候碰到一个问题,在同一个页面下跳转,路由地址不变,路由参数有变化,一开始只是在data里取路由的参数,发现根本取不到变化的路由参数. 例如:订单列表也跳转详情页,跳转方法如下 & ...

  8. BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛 网络流 + 二分 + Floyd

    Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the ...

  9. 莫烦大大keras的Mnist手写识别(5)----自编码

    一.步骤: 导入包和读取数据 数据预处理 编码层和解码层的建立 + 构建模型 编译模型 训练模型 测试模型[只用编码层来画图] 二.代码: 1.导入包和读取数据 #导入相关的包 import nump ...

  10. kernel panic必备知识

    获得vmcore Kernel dump 是什么 Kdump – 捕捉kernel dump的工具 Kdump的工作原理 Kdump的配置 Dump分析的工具crash(1) 准备环境 根据vmcor ...