CString is a boon to all C++ converts of MFC. Good God! it is so much of a pain to use char* in c++. A small mistake could lead to big mishaps, to any programmer's nightmare. CString in MFC gives lot many features to handle strings, weaning away all such bad memories of char *. This article deals with the usage of CString in MFC programs. It also tries to give some small code snippets wherever necessary.
CString Features:
There are some special features in CString that makes it really unique and attractive. Some of them are
- CString has no base classes in MFC. So it is light weight.
- CString is written based on TCHAR type. It can automatically support UNICODE as well as ASCII strings.
- CString implements a reference counting mechanism for copy of objects. This saves a lot of memory usage.
How to Use CString:
CString can be initialized by calling one of its constructors. It can be instantiated with or without parameters. The constructors can accept a char, char*, TCHAR, wchar types as a parameter. The string is constructed according to the parameter passed.
In case it is created without any parameters, the string will be empty. Values can be assigned with the use of '=' operator.
CString strExample;
strExample = "CString sample value";
To get the length of a CString, the function CString.GetLength() can be used. This returns the length of the string as an integer.
String Concatenation with a CString:
String concatenation is very easy with CString. CString has an overloaded operator for + sign, which makes a programmer's life easier.
CString strExample1 = "Sample Value ";
CString strExample2 = "for concat with CString";
//Concatenation with CString and CString
strExample1 = strExample1 + strExample2;
//Concatenation with CString and a const char*
strExample1 = strExample1 + "for concat without CString";
CString and BSTR:
CString also provides for conversions to BSTR with functions like AllocSysString and SetSysString. It can allocate memory and copy the data into the allocated space for BSTR.
CString strBstrConv = "Sample for CString to BSTR";
BSTR bstrVariable = strBstrConv.AllocSysString();
String Comparisons with CString:
CString contains an operator member function for the '=' operator. This operator performs a case-sensitive comparison. For case-insensitive comparisons the function CompareNoCase can be used.
CString strCompareString1 = "String Compare Sample";
CString strCompareString2 = "sTring Compare Sample";
//Using == operator case sensitive CString comparison
if(strCompareString1 == strCompareString2)
MessageBox("Both Strings are equal - Case Sensitive");
//Using CompareNoCase for case-insensitive CString comparison
if(strCompareString1.ComparNoCase(strCompareString2) == 0)
MessageBox("Both Strings are equal - Case Insensitive");
Finding a Character inside CString:
CString supports find for a single character and also strings. It provides searching a character from the reverse direction.
CString strFindData = "Finding a Character and a string in forward direction";
//Find a char 'C'
int lPos = strFindData.Find('C');
//Find "string"
int lPos = strFindData.Find('string');
These find functions return the position of the character or string as an integer.
Using Format Functions in CString:
The Format function in CString can be used to convert different data types into a string type. This function is similar to the sprintf function.
CString strFormatData;
int x = 40;
char strdata[] = "Test Data ";
strFormatData.Format("%s %d",x,strdata);
//The output will be "Test Data 40"
The above is only a small list of the capabilities of CString. There are a lot more of features available with CString. Not all of them can be explained. More of those features will become clear when one starts using it.