The help you needed in C, C++, MFC
Complete tutorials with free source code
PDF Print E-mail

CString class

In Part 3, you learnt how to use CString in win32 applications. For MFC applications, you can straightaway use it. In this page, you can find out what you can do with CStrings.

Creating and assigning strings to CString

CString s;
s = TEXT("hello!!!");

The above 2 lines created a CString object s and assigned the string "hello!!!" to it. TEXT macro is used because if you are using UNICODE or not, the code will work as explained in Part 2.

Appending another string

CString path = TEXT("C:\\");
path.Append(TEXT("WINDOWS"));
//now path contains "C:\\WINDOWS"

Inserting another string to middle of a string

CString s=TEXT("how you");
s.Insert(4, TEXT("are "));
//now s contains "how are you"

The first argument to Insert is the position to insert new string.

Truncating a string

CString name = TEXT("Tom Sawyer");
name.Truncate(3);
//now name contains only first 3 characters, "Tom"

Formatting a string

int age = 20;
CString s;
s.Format(TEXT("The age is: %d"), age);
//now s contains "The age is 20"

The formatting characters (%d, %c, %u etc.) are the same as in printf.

Finding the length of the string

CString s = TEXT("hello");
int length = s.GetLength();
//now, length contains the number of characters in "hello", 5

Comparing 2 strings

CString s1, s2;
s1 = TEXT("abcd");
s2 = TEXT("efgh");
if(s1.Compare(s2) == 0)
MessageBox(NULL, TEXT("s1 and s2 are equal"), TEXT("message"), NULL);else
MessageBox(NULL, TEXT("s1 and s2 are not equal"), TEXT("message"), NULL);

As you can see in the above code, Compare will return 0 if the strings are equal or return nonzero number if not equal.

Finding the position of a character or sub string in a string

CString string = TEXT("how are you");
int pos = string.Find(TEXT("are"));

Now pos will contain the index of the sub string, 4. If the sub string couldn't be found in the string, Find will return -1. You can specify Find the position to start searching if you want, as the optional second argument.

Finding the last occurrence of a string

CString string = TEXT("hello world");
int pos = string.ReverseFind('o');

Now pos will contain 7 because the index of the last 'o' in the string is 7.

Replacing a sub string with another string

CString string = TEXT("I'll come on monday");
string.Replace(TEXT("monday"), TEXT("tuesday"));
//now string will contain "I'll come on tuesday"

Note that every occurrence of the search sub string will be replaced.

Taking the last i number of characters

CString string = TEXT("Tom Sawyer");
CString s = string.Right(5);
//now s will contain the last 5 characters of "Tom Sawyer", "Sawyer"

You can take the first i number of characters in a string with function Left().

Convert a string to uppercase or lowercase

CString string = TEXT("aBcDEfG");
string.MakeUpper();
//now string will contain "ABCDEFG"
string.MakeLower();
//now string will contain "abcdefg"

Trimming a string

CString string = TEXT("***hello***");
string.Trim("*");
//now string will contain only "hello"

If you need to trim only the right side of the string, you can use TrimRight(). Similarly, if you need to trim only the left side of the string, you can use TrimLeft().

Taking the i'th character in a string

CString string = TEXT("abcdefg");
char c = string.GetAt(3);
//now c will contain 'd'

Finding whether the string is empty

CString string = TEXT("hello");
if(string.IsEmpty())
{
//string is empty
}

Simply, IsEmpty() will return true if string is empty and false if string is not empty.

There are many other functions provided by the CString class. The functions described above are the basic and most useful functions. You can deal with strings very easily using these functions. You can do some complex operations in very few lines of code.

For an example, think you need to check whether the file extension of a path is "sys".

//variable 'path' contains the path of the selected file.
//e.g.: "C:/drv/driver.sys"

if(path.Right(4).MakeUpper().Compare(TEXT(".SYS")) == 0)
{
//file extension is "sys"
}

As shown above, you can do that with just a single line. we first change the string to upper case to make the comparison case insensitive.

Most of the win32 API functions which accepts LPCTSTR (constant string of TCHAR) or LPTSTR (string of TCHAR) as string arguments. You can send a CString string as those arguments without a problem.

For an example, DeleteFile() accepts the path of the file to be deleted as LPCTSTR. But, you can send a CString as the argument without a problem.

CString path = TEXT("C:\\abc.txt");
DeleteFile(path);

Another great help CString provides you is if you write a function which accepts a CString variable, you can send just TEXT to that function. The string you send is automatically assigned to CString.

Also, in some win32 API functions, they ask for a pointer to buffer to send us a string back. We can take the string those functions send directly to a CString. For an example, take the API function GetWindowsDirectory which asks for a buffer to put the windows directory path.

CString windir;
GetWindowsDirectory(windir.GetBuffer(MAX_PATH), MAX_PATH);
windir.ReleaseBuffer();
//now windir contains the windows directory path

Here, MAX_PATH is a constant which is equal to the maximum possible length of a path of a file in Windows. Remember to call ReleaseBuffer() after you have called GetBuffer().