2024年4月19日发(作者:)

使用所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_(_T("我是中国人!"));

dui_(_T("我爱中国!"));

2 Assign(LPCSTR pstr ,int nLength ) 在pstr字符串的nLength位置后的东西全部剪切掉不要;

3 Format(LPCSTR pstrformat, ...); 按照pstrformat字符串的格式,导入其他类型变量

CDuiString folde_path; folde_(_T("%ls"), std_string);

4 GetLength()采集一个变量的宽度(大小);

gth();

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

(config, gth()-1);

我们应该注意到,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 = r->GetUserData();

std::string download_link_str = StringFromLPCTSTR(download_a());