想在一个字符串中取一个子串,如:
CString s="123456";
如何取出“45”子串。

解决方案 »

  1.   

    int pos = s.Find(szSub);
    LPCTSTR  pbuff=(LPCTSTR)s;
    if (pos != -1)
    {
        CString  szRet(&pbuff[pos], szSub.GetLength());
        return szRet;
    }
    return "";
      

  2.   

    s.Mid(3,2);CString::MidCString Mid( int nFirst ) const;
    throw( CMemoryException );CString Mid( int nFirst, int nCount ) const;
    throw( CMemoryException );Return ValueA CString object that contains a copy of the specified range of characters. Note that the returned CString object may be empty. ParametersnFirstThe zero-based index of the first character in this CString object that is to be included in the extracted substring.nCountThe number of characters to extract from this CString object. If this parameter is not supplied, then the remainder of the string is extracted.ResExtracts a substring of length nCount characters from this CString object, starting at position nFirst (zero-based). The function returns a copy of the extracted substring. Mid is similar to the Basic MID$ function (except that indexes are zero-based).For multibyte character sets (MBCS), nCount refers to each 8-bit character; that is, a lead and trail byte in one multibyte character are counted as two characters.ExampleThe following example demonstrates the use of CString::Mid.// example for CString::Mid
    CString s( _T("abcdef") );
    ASSERT( s.Mid( 2, 3 ) == _T("cde") );出处
    msdn:// .../vs98/reference/mfc/mfc classes