CString 用法总结

1.  构造一个CString对象 

   (1) 使用字符串常量       

CString str2 (_T("Hello Window!"));
CString str1 = _T("Hello Window!");

  (2) 使用字符串数组

TCHAR szBuf[] = _T("Hello Window!");
	
CString str2(szBuf);
CString str1 = szBuf;

  (3)使用指向字符串的指针          

TCHAR * pszStr = _T("Hello Window!");
	
CString str2(pszStr);
CString str1 = pszStr;

  (4) 使用CString对象

CString str(_T("Hello Window!"));
	
CString str2(str);
CString str1 = str;

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Visual Studio中,可以使用几种方法将std::string转换为CString。 第一种方法是使用CString的构造函数,将std::string作为参数传递给构造函数即可。例如,可以这样写:CString c_name(str.c_str()); 这样可以将std::string对象str转换为CString对象c_name。 第二种方法是使用CStringA或CStringW类的静态成员函数,可以根据std::string的编码类型选择不同的函数。例如,如果std::string是ASCII编码,可以使用CStringA::FromString函数来进行转换。如下所示:CStringA c_name = CStringA::FromString(str); 第三种方法是使用ATL宏CA2T,它可以将std::string转换为TCHAR*类型,然后使用CString的构造函数来创建CString对象。例如,可以这样写:CString c_name = CA2T(str.c_str()); 这样可以将std::string对象str转换为CString对象c_name。 总结来说,可以通过使用CString的构造函数,使用CStringA或CStringW类的静态成员函数,或使用ATL宏CA2T来实现std::string转换为CString。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Unicode字符集---MFC中CString与std::string转换](https://blog.csdn.net/m0_64703894/article/details/127048495)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [MFC中std::string与CString的相互转换](https://blog.csdn.net/weixin_42447188/article/details/108061523)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值