Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


}

Besides the Collate() method, the CString class provides the Compare() method to
perform a case-sensitive comparison of characters of two strings. Its syntax is:

int Compare(LPCTSTR lpsz) const;

This method compares the character at a position x to the character at the same position x
of the other, lpsz, string. The approach used to perform the comparisons is the same with
the difference that the Compare() method does not refer the user’s Regional Settings.

9.8.2 Case Insensitivity.................................................................................


As seen above, the Collate() method considers the cases of characters during comparison
and refers to the Regional Settings of the user. If you just want a simple comparison to
find if two strings are identical regardless of the cases of their characters, you can call the
CString::CollateNoCase() method. Its syntax is;

int CollateNoCase(LPCTSTR lpsz) const;

This member function considers that the alphabet is made of characters of a single case,
implying that the character a is exactly the same as the character A. The other symbols
and digits are considered “as is”. It proceeds as follows:

?? The character at a position x of one string is compared with the corresponding
character at the same position on the other string. If both characters are the
same, the comparison continues with the next character. If all characters at the
same positions on both strings are the same and the strings have the same length,
this method returns 0, indicating that the strings are identical:

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

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

int Result = CS.CollateNoCase(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