CString

______CString::Left.

Return Value:   CString object containing a copy of the specified range of characters. Note that the returnedCString object may be empty.

// example for CString::Left
CString s( _T("abcdef") );
ASSERT( s.Left(2) == _T("ab") );

//int GetLength( ) const;The count does not include a null terminator.
int c = s.GetLength();
//LPTSTR GetBuffer( int nMinBufLength );
nMinBufLength does not include a null terminator.
LPTSTR p = s.GetBuffer(c);

Returns a pointer to the internal character buffer for the CString object. The returned LPTSTR is not const and thus allows direct modification of CString contents.

If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions.

The address returned by GetBuffer may not be valid after the call to ReleaseBuffer since additional CStringoperations may cause the CString buffer to be reallocated. The buffer will not be reallocated if you do not change the length of the CString.

The buffer memory will be freed automatically when the CString object is destroyed.

Note that if you keep track of the string length yourself, you should not append the terminating null character. You must, however, specify the final string length when you release the buffer with ReleaseBuffer. If you do append a terminating null character, you should pass –1 for the length to ReleaseBuffer and ReleaseBuffer will perform astrlen on the buffer to determine its length.

Example

The following example demonstrates the use of CString::GetBuffer.

// example for CString::GetBuffer
CString s( "abcd" );
#ifdef _DEBUG
afxDump << "CString s " << s << "
";
#endif
LPTSTR p = s.GetBuffer( 10 );
strcpy( p, "Hello" );   // directly access CString buffer
s.ReleaseBuffer( );
#ifdef _DEBUG
afxDump << "CString s " << s << "
";
#endif
 
原文地址:https://www.cnblogs.com/aprilapril/p/3224086.html