Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 9 Strings Visual C++ and MFC Fundamentals


9.7 Operations on Strings...................................................................................


9.7.1 About Constructing a String..............................................................


The constructors we have used so far were meant to initialize a string when declaring it.
Normally, if you do not have an initial value when declaring the string variable, you can
use the default constructor:

CString();

The default constructor is used to declare an empty string that can be initialized later on.
If thevariable already contains a value but you want it to be empty, you can call the
CString::Empty() method. Its syntax is:

void Empty();

This method removes all characters, if there are any, from a string variable and restores
the memory it was using. If you think the string contains empty spaces to its left side and
you think this could compromise an operation on the string, to remove such empty
spaces, call the TrimLeft() method whose syntax is:

void TrimLeft();

To remove empty spaces from the right side of a string, call the TrimRight() method. Its
syntax is:

void TrimRight();

If at one time you want to find out whether a string is empty or not, call the
CString::IsEmpty() method. Its syntax is:

BOOL IsEmpty() const

This method returns TRUE if the string is empty. Otherwise, it returns FALSE.

9.7.2 String Assignment...............................................................................


We saw earlier that you can declare an empty CString variable using the default
constructor. To initialize it, you can assign it a string value of your choice. Here is an
example:

void CExoView::OnDraw(CDC* pDC)
Free download pdf