使用所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. 关于TJOI2014的一道题——Alice and Bob

    B Alice and Bob •输入输出文件: alice.in/alice.out •源文件名: alice.cpp/alice.c/alice.pas • 时间限制: 1s 内存限制: 128M ...

  2. java 微信api开发

    最近使用了一个很好的微信api框架,比较好使. 源码地址:https://github.com/chanjarster/weixin-java-tools/wiki 微信公众平台:微信公众平台开发文档 ...

  3. SQLServer 在存储过程里使用事务控制的简单小例子

    alter proc sp_test(     @name varchar(50))asbegin    --开始事务   begin transaction   --设置一个存储报错代码的变量   ...

  4. 拖入浏览器读取文件demo

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Emmet(Zen Coding)语法规则简介

    ———Emmet(Zen Coding)语法规则简介——— [Zen Coding可谓快速开发HTML和CSS的利器,主要采用仿css类选择器方式编写代码,以下是该利器的基本语法规则和代码示例] 基础 ...

  6. C/C++关键字

    1. static关键字 作用 在函数体内静态变量具有记忆功能.在函数体内定义的静态变量离开时不会被清除,在下次函数调用的时候其值保持不变. 限制变量或函数的使用范围.static修饰的全局变量或者函 ...

  7. ASP.NET MD5加密

    protected void Button1_Click(object sender, EventArgs e) { string pwd = TextBox2.Text.Trim(); Respon ...

  8. Uoj 52. 【UR #4】元旦激光炮 神题+交互题

    Code: #include "kth.h" #include<iostream> int minn(int x,int y){return x<y?x:y;}; ...

  9. 移动端 配置rem

    <script> function Rem() { var docEl = document.documentElement, oSize = docEl.clientWidth / 7. ...

  10. 洛谷P1307 数字反转【水题】

    给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零(参见样例2). 输入输出格式 输入格式: 一个整数 NN ...