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

String classes

So far in this tutorial, we have seen only simple strings which are not that easy to manipulate. You have to use functions like strcpy, strcat etc to manipulate strings and that is somewhat difficult. The good thing is, you do not want to use those simple strings that much because there are better string types which you can use easily.

Most popular string types available are string and CString classes.

Where the string class is used most of time is in iostream programming, those programs you write in the first days of your programming life. The string can be used in the same way as other data types such as int, char, etc. Here is an example.

#include
#include
using namespace std;

int main()
{
string s;
cout<<"Enter your name? ";
getline(cin, s);
cout<<"Hello "<<<".\n";
return 0;
}

The other string type which you will find very convenient is CString. When you got familiar with it, you will not even think about using other string types. It is automatically included to your MFC projects. You can include CString to win32 projects too. To do that, follow following steps.

  1. Create win32 project with default settings of the wizard.
  2. Replace the stdafx.h header provided to you with this file.
  3. Select project name in the solution explorer pane, which is in the left of the window and go to project->properties in the main menu.
  4. In configuration properties->C/C++->Code generation, change Runtime Library to Multi-Threaded (/MT)

Now, you should be able to compile the project without a problem. If you want to build the application in release mode, then you need to repeat the 3 and 4 steps because there are 2 settings for debug and release modes.

In Part 4, you can see the basic operations on CString s.