ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/dv_vclib/html/5e352fc1-cdf9-4989-892e-c29a21f5c970.htm
Shared Visual C++ Classes Reference
CStringT::operator +=
Example See Also Send Feedback
Concatenates characters to the end of the string.
CStringT& operator+=(
const CThisSimpleString& str
);
template<
bool bMFCDLL
>
CStringT& operator+=(
const const CSimpleStringT<BaseType, bMFCDLL>& str
);
template<
int t_nSize
>
CStringT& operator+=(
const CStaticString< XCHAR, t_nSize >& strSrc
);
CStringT& operator+=(
PCXSTR pszSrc
);
CStringT& operator+=(
PCYSTR pszSrc
);
CStringT& operator+=(
char ch
);
CStringT& operator+=(
unsigned char ch
);
CStringT& operator+=(
wchar_t ch
);
CStringT& operator+=(
const VARIANT& var
);
Parameters
str
A reference to a CThisSimpleString object.
bMFCDLL
A boolean specifying whether the project is an MFC DLL or not.
BaseType
The string base type.
var
A variant object to concatenate to this string.
ch
An ANSI or Unicode character to concatenate with a string.
pszSrc
A pointer to the original string being concatenated.
strSrc
A CStringT to concatenate to this string.
Remarks
The operator accepts another CStringT object, a character pointer, or a single character. You should be aware that memory exceptions can occur whenever you use this concatenation operator because new storage can be allocated for characters added to this CStringT object.
For information on CThisSimpleString, see the Remarks section of CStringT::CStringT.
Note:
Although it is possible to create CStringT instances that contain embedded null characters, we recommend against it. Calling methods and operators on CStringT objects that contain embedded null characters can produce unintended results.
Example
Visual C++ Copy Code
// typedef CStringT<TCHAR, StrTraitATL<TCHAR, ChTraitsCRT<TCHAR>>> CAtlString;
CAtlString s(_T("abc"));
ASSERT((s += _T("def")) == _T("abcdef"));
Requirements
Header: cstringt.h
See Also
Concepts
CStringT Class
CStringT Members
Send feedback on this topic to Microsoft.
ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/dv_vclib/html/39569564-290e-4b6f-a829-2919556bee32.htm
Shared Visual C++ Classes Reference
CStringT::operator +
Example See Also Send Feedback
Concatenates two strings or a character and a string.
friend CStringT operator+(
const CStringT& str1,
const CStringT& str2
);
friend CStringT operator+(
const CStringT& str1,
PCXSTR psz2
);
friend CStringT operator+(
PCXSTR psz1
const CStringT& str2,
);
friend CStringT operator+(
char ch1
const CStringT& str2,
);
friend CStringT operator+(
const CStringT& str1,
char ch2
);
friend CStringT operator+(
const CStringT& str1,
wchar_t ch2
);
friend CStringT operator+(
wchar_t ch1
const CStringT& str2,
);
Parameters
ch1
An ANSI or Unicode character to concatenate with a string.
ch2
An ANSI or Unicode character to concatenate with a string.
str1
A CStringT to concatenate with a string or character.
str2
A CStringT to concatenate with a string or character.
psz1
A pointer to a null-terminated string to concatenate with a string or character.
psz2
A pointer to a string to concatenate with a string or character.
Remarks
There are seven overload forms of the CStringT::operator+ function. The first version concatenates two existing CStringT objects. The next two concatenate a CStringT object and a null-terminated string. The next two concatenate a CStringT object and an ANSI character. The last two concatenate a CStringT object and a Unicode character.
Note:
Although it is possible to create CStringT instances that contain embedded null characters, we recommend against it. Calling methods and operators on CStringT objects that contain embedded null characters can produce unintended results.
Example
Visual C++ Copy Code
// typedef CStringT<TCHAR, StrTraitATL<TCHAR, ChTraitsCRT<TCHAR>>> CAtlString;
CAtlString s1(_T("dog ")), s2(_T(" awake")), s3; // Empty CAtlString objects
s1= _T("The ") + s1;
s3= s1 + _T('i');
s3= s3 + _T('s');
s3= s3 + s2;
ASSERT(s3 == _T("The dog is awake"));
Requirements
Header: cstringt.h
See Also
Concepts
CStringT Class
CStringT Members
Send feedback on this topic to Microsoft.
查MSDN是Windows程序员必须掌握的技能之一。
ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/dv_vclib/html/41db66b2-9427-4bb3-845a-9b6869159a6c.htm
MFC Library Reference
Basic CString Operations
See Also Send Feedback
This article explains basic CString operations, including:
Creating CString objects from standard C literal strings
Accessing individual characters in a CString
Concatenating two CString objects
Comparing CString objects
The CString class provides member functions and overloaded operators that duplicate and, in some cases, surpass the string services of the C run-time libraries (for example, strcat_s).
Creating CString Objects from Standard C Literal Strings
You can assign C-style literal strings to a CString just as you can assign one CString object to another:
Assign the value of a C literal string to a CString object:
Visual C++ Copy Code
CString myString = _T("This is a test");
Assign the value of one CString to another CString object:
Visual C++ Copy Code
CString oldString = _T("This is a test");
CString newString = oldString;
The contents of a CString object are copied when one CString object is assigned to another. Thus, the two strings do not share a reference to the actual characters that make up the string. For more information on using CString objects as values, see the article CString Semantics.
Note:
To write your application so that it can be compiled for Unicode or for ANSI, code literal strings using the _T macro. For more information, see the article Unicode and Multibyte Character Set (MBCS) Support.
Accessing Individual Characters in a CString
You can access individual characters within a CString object with the GetAt and SetAt member functions. You can also use the array element, or subscript, operator ( [ ] ) instead of GetAt to get individual characters (this is similar to accessing array elements by index, as in standard C-style strings). Index values for CString characters are zero based.
Concatenating Two CString Objects
To concatenate two CString objects, use the concatenation operators (+ or +=) as follows:
Visual C++ Copy Code
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".
At least one argument to the concatenation operators (+ or +=) must be a CString object, but you can use a constant character string (such as "big") or a char (such as 'x') for the other argument.
Comparing CString Objects
The Compare member function and the == operator for CString are equivalent. Compare, operator==, and CompareNoCase are MBCS and Unicode aware; CompareNoCase is also case insensitive. The Collate member function of CString is locale sensitive and is often slower than Compare. Collate should be used only where it is necessary to abide by the sorting rules as specified by the current locale.
The following table shows the available CString comparison functions and their equivalent Unicode/MBCS-portable functions in the C run-time library:
CString function
MBCS function
Unicode function
Compare
_mbscmp
wcscmp
CompareNoCase
_mbsicmp
_wcsicmp
Collate
_mbscoll
wcscoll
The CString class overrides the relational operators (<, <=, >=, >, ==, and !=).You can compare two CStrings using these operators, as shown here:
Visual C++ Copy Code
CString s1(_T("Tom"));
CString s2(_T("Jerry"));
ASSERT(s2 < s1);
See Also
Concepts
Strings (ATL/MFC)
Send feedback on this topic to Microsoft.