返回值:如果CString 對象的長度爲0,則返回非零值;否則返回0。
說明:此成員函數用來測試一個CString 對象是否是空的。
CString Left( int nCount ) const;
ASSERT( s.Left(2) == _T("ab") );
說明: 此成員函數用來讀取一個由nID 標識的Windows 字符串資源,並放入一個已有CString 對象中。
下面的例子說明了如何使用CString::LoadString。
if (! s.LoadString( IDS_FILENOTFOUND ))
示例: CString s( _T("abcdef") ); ASSERT( s.Mid( 2, 3 ) == _T("cde") );
void ReleaseBuffer( int nNewLength = -1 );
此字符串的以字符數表示的新長度,不計算結尾的空字符。如果這個字
符串是以空字符結尾的,則參數的缺省值-1 將把CString 的大小設置爲
使用ReleaseBuffer 來結束對由GetBuffer 分配的緩衝區的使用。如果你知道緩
衝區中的字符串是以空字符結尾的,則可以省略nNewLength 參數。如果字符
串不是以空字符結尾的,則可以使用nNewLength 指定字符串的長度。在調用
ReleaseBuffer 或其它CString 操作之後,由GetBuffer 返回的地址是無效的。
下面的例子說明了如何使用CString::ReleaseBuffer。
LPTSTR p = s.GetBuffer( 1024 );
ASSERT( s.GetLength() == 3 ); // 字符串長度 = 3
s.ReleaseBuffer(); // 釋放多餘的內存,現在p 無效。
ASSERT( s.GetLength() == 3 ); // 長度仍然是3
int CString::Remove ( TCHAR ch );
返回值:返回從字符串中移走的字符數。如果字符串沒有改變則返回零。
說明:此成員函數用來將ch 實例從字符串中移走。與這個字符的比較是區分大小寫
CString str (“This is a test.”);
ASSERT( str ==“This is a es. ” );
int Replace( TCHAR chOld, TCHAR chNew );
int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );
返回值:返回被替換的字符數。如果這個字符串沒有改變則返回零。
lpszOld 一個指向字符串的指針,該字符串包含了要被lpszNew 替換的字符。
LpszNew 一個指向字符串的指針,該字符串包含了要用來替換lpszOld 的字符。
說明:此成員函數用一個字符替換另一個字符。函數的第一個原形在字符串中用chNew
現場替換chOld。函數的第二個原形用lpszNew 指定的字符串替換lpszOld 指定
在替換之後,該字符串有可能增長或縮短;那是因爲lpszNew 和lpszOld 的長度
int n = strZap.Replace('-', '+' );
CString strBang( “Everybody likes ice hockey” );
n = strBang.Replace( “hockey”, “golf” );
n = strBang.Replace ( “likes” , “plays” );
n = strBang.Replace( “ice”, NULL );
ASSERT( strBang == “Everybody plays golg” );
// 要移走這個額外的空格,可以將它包括在要被替換的字符串中,例如,“ice ”。
int ReverseFind( TCHAR ch ) const;
返回值: 返回此CString 對象中與要求的字符匹配的最後一個字符的索引;如果沒有找
說明:此成員函數在此CString 對象中搜索與一個子串匹配的最後一個字符。此函數
ASSERT( s.ReverseFind( 'b' ) == 4 );
CString Right( int nCount ) const;
ASSERT( s.Right(2) == _T("ef") );
void SetAt( int nIndex, TCHAR ch );
說明:可以把字符串理解爲一個數組,SetAt類似於[].注意nIndex的範圍,如果不合適會有調試錯誤。 Ch 更替字符, 把nIndex位置上的字符 變成ch
void CString::TrimLeft( TCHAR chTarget );
說明:如果沒有參數,從左刪除字符(/n/t空格等),至到遇到一個非此類字符. 當然你也可以指定刪除那些字符. 如果指定的參數是字符串,那麼遇上其中的一個字符就刪除. /n 換行符 /t TAB字符
CString str = "abbcadbabcadb ";
void CString::TrimRight( TCHAR chTarget );
void CString::TrimRight( LPCTSTR lpszTargets );
int Compare( LPCTSTR lpsz ) const;
返回值:字符串一樣返回0,小於lpsz 返回-1,大於lpsz 返回1, 區分大小字符
ASSERT( s1.Compare( s2 ) == -1 );
ASSERT( s1.Compare( "abe" ) == -1
int CompareNoCase( LPCTSTR lpsz ) const;
返回值: 字符串一樣 返回0,小於lpsz 返回-1,大於lpsz 返回1,不區分大小字符
int Collate( LPCTSTR lpsz ) const;
int CollateNocase( LPCTSTR lpsz ) const;
CString( const CString& stringSrc );
CString( TCHAR ch, int nRepeat = 1 );
CString( LPCTSTR lpch, int nLength );
CString( const unsigned char* psz );
CString s5( 'x' ); // s5 = "x"
CString s6( 'x', 6 ); // s6 = "xxxxxx"
CString s7((LPCSTR)ID_FILE_NEW); // s7 = "Create a new document"
CString city = "Philadelphia";
int Delete( int nIndex, int nCount = 1);
如果nCount(3) > GetCount() – nIndex (5-2)就會執行錯誤
ASSERT( s.GetLength( ) == 0 );
int Find( LPCTSTR lpszSub ) const;
int Find( TCHAR ch, int nStart ) const;
int Find( LPCTSTR lpszSub, int nStart ) const;
返回值: 不匹配的話返回 -1; 索引以0 開始; nStar 代表以索引值nStart 的字符開始搜索 ,
ASSERT( s.Find( "de" ) == 3 );
Cstring str(“The stars are aligned”);
int FindOneOf( LPCTSTR lpszCharSet ) const;
注意::返回此字符串中第一個在lpszCharSet中也包括字符並且從零開始的索引值
ASSERT( s.FindOneOf( "xd" ) == 3 ); // 'd' is first match.
void Format( LPCTSTR lpszFormat, ... );
void Format( UINT nFormatID, ... );
TCHAR GetAt( int nIndex ) const;
返回值:返回標號爲nIndex的字符,你可以把字符串理解爲一個數組,GetAt類似於[].注意nIndex的範圍,如果不合適會有調試錯誤。
LPTSTR GetBuffer( int nMinBufLength );
返回值:一個指向對象的(以空字符結尾的)字符緩衝區的LPTSTR 指針。
字符緩衝區的以字符數表示的最小容量。這個值不包括一個結尾的空字符的空間。
afxDump << "CString s " << s << "/n";
strcpy( p, "Hello" ); // 直接訪問CString 對象。
afxDump << "CString s " << s << "/n";
說明:此成員函數用來獲取這個CString 對象中的字節計數。這個計數不包括結尾的空字符。
對於多字節字符集(MBCS),GetLength 按每一個8 位字符計數;即,在一個多字節字符中的開始和結尾字節被算作兩個字節。
下面的例子說明了如何使用CString::GetLength。
int Insert( int nIndex, TCHAR ch );
int Insert( int nIndex, LPCTSTR pstr );
返回值:返回修改後的長度,nIndex是字符(或字符串)插入後的索引號例子
int n = str.Insert( 6, “is” );
ASSERT( n == str.GetLength( ) );
printf( “1: %s/n”, ( LPCTSTR ) str );
ASSERT( n == str.GetLength( ) );
printf ( “2: %s/n”, (LPCTSTR) STR );
ASSERT( n == str.GetLength ( ) );