|
|
|
CString classIn 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; 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:\\"); Inserting another string to middle of a string CString s=TEXT("how you"); The first argument to Insert is the position to insert new string. Truncating a string CString name = TEXT("Tom Sawyer"); Formatting a string int age = 20; The formatting characters (%d, %c, %u etc.) are the same as in printf. Finding the length of the string CString s = TEXT("hello"); Comparing 2 strings CString s1, s2; 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"); 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"); 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"); Note that every occurrence of the search sub string will be replaced. Taking the last i number of characters CString string = TEXT("Tom 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"); Trimming a string CString string = TEXT("***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"); Finding whether the string is empty CString string = TEXT("hello"); 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. 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"); 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; 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(). |