CString join method?
I need to concatenate a list of MFC CString objects into a single CSV string. .NET has String.Join
for this task. Is t开发者_C百科here an established way to do this in MFC/C++?
The +
operator is overloaded to allow string concatenation. I'd suggest take a look at the documentation on MSDN:
Basic CString Operations has the following example:
CString s1 = _T("This "); // Cascading concatenation
s1 += _T("is a ");
CString s2 = _T("test");
CString message = s1 + _T("big ") + s2;
// Message contains "This is a big test".
If you want the strings to be comma-separated, just add the commas yourself.
Iterate through the list of CString objects invoking the AppendFormat method.
// Initialize CStringList
CStringList cslist ;
cslist.AddTail( "yaba" ) ;
cslist.AddTail( "daba" ) ;
cslist.AddTail( "doo" ) ;
// Join
CString csv ;
for ( POSITION pos = cslist.GetHeadPosition() ; pos != NULL ; )
csv.AppendFormat( ",%s" , cslist.GetNext( pos ) ) ;
csv.Delete( 0 ) ; // remove leading comma
精彩评论