Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 9 Strings Visual C++ and MFC Fundamentals


If a character is an uppercase letter, it would be converted to lowercase. If a character is
already in lowercase, it be left in lowercase. The digits and other characters are left “as
is”.

9.8 String Comparisons......................................................................................


9.8.1 Case Sensitivity....................................................................................


String comparison consists of finding out whether the values of two strings are identical
or not. Toperform such a comparison, the CString class provides the Collate() method.
Its syntax is:

int Collate(LPCTSTR lpsz) const;

The string that called this method is compared with the lpsx argument, character by
character and with regards to the case of each combination of charcaters. To perform the
comparison, this method refers to the Regional Settings of Control Panel on the user’s
computer concerning the numeric, the date, the time, and the currency systems used

The comparison starts with the left character for most latin languages, including US
English:

?? If the character at the first position ([0]) of the string (the string that called this
method) has the same ANSI value as the first character at position [0] of the
other string (the lpsz argument), the method skips this position and continues the
comparison with the next position of characters. If all characters of the
corresponding positions of both strings are exactly the same as set on the ANSI
map, this method returns 0, meaning the strings are equal

?? If a character at one position x has an ANSI value higher than the corresponding
character at the same position x on the other string, the method returns a positive
value and stops the comparison

?? If a character at one position x has an ANSI value lower than the corresponding
character at the same position x on the other string, the method returns a
negative value and stops the comparison
Here is an example:

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

CString CS = "Charles Stanley";
CString AS = "charles Stanley";

int Result = CS.Collate(AS);

if( Result > 0 )
pDC->TextOut(10, 20, "Charles Stanley is Higher than charles Stanley");
else if( Result == 0 )
pDC->TextOut(10, 20, "Charles Stanley and charles Stanley are equal");
else // if( Result < 0 )
pDC->TextOut(10, 20, "Charles Stanley is Lower than charles Stanley");
Free download pdf