Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 9 Strings Visual C++ and MFC Fundamentals


?? If a character at one position x has an ordered 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 ordered 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 YS = "Jeremy Stanley";
CString IS = "Jeremie Andy";

int Result = YS.CollateNoCase(IS);

if( Result > 0 )
pDC->TextOut(10, 20, "Jeremy Stanley is Higher than Jeremie Stanley");
else if( Result == 0 )
pDC->TextOut(10, 20, "Jeremy Stanley and Jeremie Stanley are equal");
else // if( Result < 0 )
pDC->TextOut(10, 20, "Jeremy Stanley is Lower than Jeremie Stanley");
}

Alternatively, to perform case-insensitive string comparison on CString variables, you
can use the CompareNoCase() method. Its syntax is:

int CompareNoCase(LPCTSTR lpsz) const;

The CompareNoCase() method does not refer to the user’s Regional Settings when
performing its comparison.

Besides the CollateNoCase() and the CompareNoCase() methods, the CString class
provides an easier and probably more familiar means of performing value comparisons.
Character insensitive comparisons on CString variables can be performed using Boolean
operators ==, !=, <, <=, >, >=. Any of these operators can be applied on two CString
Free download pdf