Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 10: Characteristics of a Window's Frame


basic_string& append(size_type n, E c);
basic_string& append(const_iterator first, const_iterator last);

In the following example, we use the first versions that takes a string variable as
argument and add the value of that argument to the right side of the string variable that
called it. After this version executes, it modifies the value of the string that made the call:

void CExerciseView::OnDraw(CDC* pDC)
{
CExerciseDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

string Make = "Ford ";
string Model = "Explorer";

string Car = Make.append(Model);
int LengthCar = Car.length();

int LengthMake = Make.length();
int LengthModel = Model.length();

pDC->TextOut(10, 10, Make.c_str(), LengthMake);
pDC->TextOut(10, 30, Model.c_str(), LengthModel);
pDC->TextOut(10, 50, Car.c_str(), LengthCar);
}

If you want to append only a specific number of characters, use the second version of the
append() method.

9.3 The Characters of a String..........................................................................


9.3.1 Access to Characters...........................................................................


As mentioned earlier, the characters of a null-terminated string are stored in an array. To
access an individual character of a null-terminated string, use the square brackets
operator. For example, if you had declared and initialized a string using

char FirstName[] = _T("Arsene");

you can access the third character using FirstName[2];

The strchr() function looks for the first occurrence of a certain character in a null-
terminated string. Its syntax is:
Free download pdf