C++ 如何连接多个CString
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3159194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to concatenate multiple CString
提问by Gustavo V
All functions return CString, this is a MFC code and must compile in 32 & 64 bits.
所有函数都返回 CString,这是一个 MFC 代码,必须以 32 位和 64 位编译。
Currently I'm using
目前我正在使用
CString sURI = GetURL(); sURI += GetMethod(); sURI += "?"; sURI += GetParameters();
CString sURI = GetURL(); sURI += GetMethod(); sURI += "?"; sURI += GetParameters();
Exists any manner to do the same like:
存在任何方式来做同样的事情:
CString sURI = GetURL() + GetMethod() + "?" + GetParameters();
CString sURI = GetURL() + GetMethod() + "?" + GetParameters();
回答by Bojan Hrnkas
Problem is that "?" of type "const char*" is, and its + operator does not take right hand operand of type CString. You have to convert "?" to CString like this:
问题是“?” 类型为“const char*”的是,其 + 运算符不采用 CString 类型的右手操作数。你必须转换“?” 到 CString 像这样:
CString sURI = GetURL() + GetMethod() + _T("?") + GetParameters();
回答by Luca Matteis
As long as all those functions return a CString
object, then it should be fine to use the +
operator for concatenation.
只要所有这些函数都返回一个CString
对象,那么使用+
运算符进行连接应该没问题。
Otherwise use the CString _T(const char *)
function to wrap your regular C strings and make them a CString.
否则使用该CString _T(const char *)
函数来包装您的常规 C 字符串并使它们成为 CString。