Visual C++ and MFC Fundamentals Chapter 10: Characteristics of a Window's Frame
CString Common = "Commonwealth of Virginia";
CString Sub = Common.Left(12);
pDC->TextOut(10, 20, Sub);
}
As the Left() method considers the characters from the left position, if you want the
characters of the end of a string, you can call the CString::Right() to create a new string.
Its syntax is:
CString Right( int nCount ) const;
Here is an example:
void CExerciseView::OnDraw(CDC* pDC)
{
CExerciseDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CString Common = "Commonwealth of Virginia";
CString Sub = Common.Right(8);
pDC->TextOut(10, 20, Sub);
}
Alternatively, you can create a sub string using any range of characters from a string. To
do this, call the CString::Mid() method whose syntaxes are:
CString Mid(int nFirst) const;
CString Mid(int nFirst, int nCount) const;
To create the sub string start from any character in the string, use the first version whose
nFirst argument specifies where the new string would start. In this case, the method
would return a string made of characters from the nFirst position to the last character of
the string.
To create a sub string using a specific number of characters, use the second version. The
nFirst argument specifies where the new string would start. The nCount argument is the
number of characters that would be used from the main string to create the new string.
9.6.2 Finding a Sub String............................................................................
To scan a string for a group of characters, you can call the following versions of the
CString::Find() method:
int Find(LPCTSTR lpszSub) const;
int Find(LPCTSTR pstr, int nStart) const;
To find a sub string in a string, pass the desired sub string as the lpszSub argument. As
done with the single character, you can specify the position to start looking by providing
a second argument as nStart and use the fourth version of this method. If the sub string is
found, this method returns the position of its first character in the string.
If the character or the sub string is not found, the method returns –1.