CString::Compare
int Compare( LPCTSTR lpsz ) const;
返回值 字符串一樣返回0
小于lpsz 返回-1
大于lpsz 返回1
區分大小字符
CString s1( "abc" );
CString s2( "abd" );
ASSERT( s1.Compare( s2 ) == -1 );
ASSERT( s1.Compare( "abe" ) == -1 );
CString::CompareNoCase
int CompareNoCase( LPCTSTR lpsz ) const;
返回值 字符串一樣 返回0
小于lpsz 返回-1
大于lpsz 返回1
不區分大小字符
CString::Collate
int Collate( LPCTSTR lpsz ) const;
同CString::Compare
CString::CollateNoCase
int CollateNocase( LPCTSTR lpsz ) const;
同CString::CompareNoCase
CString::CString
CString( );
CString( const CString& stringSrc );
CString( TCHAR ch, int nRepeat = 1 );
CString( LPCTSTR lpch, int nLength );
CString( const unsigned char* psz );
CString( LPCWSTR lpsz );
CString( LPCSTR lpsz );
例子最容易說(shuō)明問(wèn)題
CString s1;
CString s2( "cat" );
CString s3 = s2;
CString s4( s2 + " " + s3 );
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";
CString::Delete
int Delete( int nIndex, int nCount = 1);
返回值是被刪除前的字符串的長(cháng)度
nIndex是第一個(gè)被刪除的字符,nCount是一次刪除幾個(gè)字符。根據我實(shí)驗得出的結果:當nCount>要刪除字符串的最大長(cháng)度(GetCount() - nIndex)時(shí)會(huì )出錯,當nCount過(guò)大,沒(méi)有足夠的字符刪除時(shí),此函數不執行。
例子
CString str1,str2,str3;
char a;
str1 = "nihao";
str2 = "nIhao";
int x;
// int i=(str1 == str2);
str1.Delete(2,3);
如果nCount(3) > GetCount() – nIndex (5-2)就會(huì )執行錯誤
CString::Empty
Void Empty( );
沒(méi)有返回值 清空操作;
例子
CString s( "abc" );
s.Empty();
ASSERT( s.GetLength( ) == 0 );
CString::Find
int Find( TCHAR ch ) const;
int Find( LPCTSTR lpszSub ) const;
int Find( TCHAR ch, int nStart ) const;
int Find( LPCTSTR lpszSub, int nStart ) const;
返回值 不匹配的話(huà)返回 -1; 索引以0 開(kāi)始
nStar 代表以索引值nStart 的字符開(kāi)始搜索 ,
即為包含以索引nStart字符后的字符串
例子
CString s( "abcdef" );
ASSERT( s.Find( ‘c‘ ) == 2 );
ASSERT( s.Find( "de" ) == 3 );
Cstring str(“The stars are aligned”);
Ing n = str.Find(‘e‘,5);
ASSERT(n == 12)
CString::FindOneOf
int FindOneOf( LPCTSTR lpszCharSet ) const;
返回值 不匹配的話(huà)返回 -1; 索引以0 開(kāi)始
注意::返回此字符串中第一個(gè)在lpszCharSet中也包括字符并且從零開(kāi)始的索引值
例子
CString s( "abcdef" );
ASSERT( s.FindOneOf( "xd" ) == 3 ); // ‘d‘ is first match.
CString::Format
void Format( LPCTSTR lpszFormat, ... );
void Format( UINT nFormatID, ... );
lpszFormat 一個(gè)格式控制字符串
nFormatID 字符串標識符
例子
CString str;
Str.Format(“%d”,13);
此時(shí)Str為13
CString::GetAt
TCHAR GetAt( int nIndex ) const;
返回標號為nIndex的字符,你可以把字符串理解為一個(gè)數組,GetAt類(lèi)似于[].注意nIndex的范圍,如果不合適會(huì )有調試錯誤。
CString::GetBuffer
LPTSTR GetBuffer( int nMinBufLength );
返回值
一個(gè)指向對象的(以空字符結尾的)字符緩沖區的LPTSTR 指針。
參數
nMinBufLength
字符緩沖區的以字符數表示的最小容量。這個(gè)值不包括一個(gè)結尾的空字符的空間。
說(shuō)明
此成員函數返回一個(gè)指向CString 對象的內部字符緩沖區的指針。返回的LPTSTR 不是const,因此可以允許直接修改CString 的內容。如果你使用由GetBuffer 返回的指針來(lái)改變字符串的內容,你必須在使用其它的CString 成員函數之前調用ReleaseBuffer 函數。
在調用ReleaseBuffer 之后,由GetBuffer 返回的地址也許就無(wú)效了,因為其它的CString 操作可能會(huì )導致CString 緩沖區被重新分配。如果你沒(méi)有改變此CString 的長(cháng)度,則緩沖區不會(huì )被重新分配。當此CString 對象被銷(xiāo)毀時(shí),其緩沖區內存將被自動(dòng)釋放。
注意,如果你自己知道字符串的長(cháng)度,則你不應該添加結尾的空字符。但是,當你用ReleaseBuffer 來(lái)釋放該緩沖區時(shí),你必須指定最后的字符串長(cháng)度。如果你添加了結尾的空字符,你應該給ReleaseBuffer 的長(cháng)度參數傳遞-1 ,ReleaseBuffer 將對該緩沖區執行strlen 來(lái)確定它的長(cháng)度。
下面的例子說(shuō)明了如何用CString::GetBuffer。
// CString::GetBuffer 例子
CString s( "abcd" );
#ifdef _DEBUG
afxDump << "CString s " << s << "\n";
#endif
LPTSTR p = s.GetBuffer( 10 );
strcpy( p, "Hello" ); // 直接訪(fǎng)問(wèn)CString 對象。
s.ReleaseBuffer( );
#ifdef _DEBUG
afxDump << "CString s " << s << "\n";
#endif
CString::GetLength
int GetLength( ) const;
返回值
返回字符串中的字節計數。
說(shuō)明
此成員函數用來(lái)獲取這個(gè)CString 對象中的字節計數。這個(gè)計數不包括結尾的空字符。
對于多字節字符集(MBCS),GetLength 按每一個(gè)8 位字符計數;即,在一個(gè)多字節字符中的開(kāi)始和結尾字節被算作兩個(gè)字節。
示例
下面的例子說(shuō)明了如何使用CString::GetLength。
// CString::GetLength 示例
CString s( "abcdef" );
ASSERT( s.GetLength() == 6 );
CString::Insert
int Insert( int nIndex, TCHAR ch );
int Insert( int nIndex, LPCTSTR pstr );
返回修改后的長(cháng)度,nIndex是字符(或字符串)插入后的索引號例子
CString str( “HockeyBest”);
int n = str.Insert( 6, “is” );
ASSERT( n == str.GetLength( ) );
printf( “1: %s\n”, ( LPCTSTR ) str );
n = str.Insert( 6, ‘ ‘ );
ASSERT( n == str.GetLength( ) );
printf ( “2: %s\n”, (LPCTSTR) STR );
n = str.Insert(555, ‘1’);
ASSERT( n == str.GetLength ( ) );
printf ( “3: %s\n”, ( LPCTSTR ) str );
輸出
1. Hockeyis Best
2. Hockey is Best
3. Hockey is Best!