Share via


Strings: Basic CString Operations

OverviewHow Do I

This article explains basic 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).

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:

    CString myString = "This is a test";
    
  • Assign the value of one CString to another CString object:

    CString oldString = "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 Strings: CString Semantics.

Tip   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 Strings: 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:

CString s1 = "This ";        //Cascading concatenation
s1 += "is a ";
CString s2 = "test";
CString message = s1 + "big " + s2; 
//Message contains "This is a big test".

At least one of the arguments 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 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:

CString s1( "Tom" );
CString s2( "Jerry" );
if( s1 < s2 )
    ...