CString常用方法簡介


CString常用方法簡介

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 );

例子最容易說明問題

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); 

返回值是被刪除前的字符串的長度

nIndex是第一個被刪除的字符,nCount是一次刪除幾個字符。根據我實驗得出的結果:當nCount>要刪除字符串的最大長度(GetCount() -nIndex)時會出錯,當nCount過大,沒有足夠的字符刪除時,此函數不執行。

例子

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)就會執行錯誤

 

CString::Empty

Void Empty( );

沒有返回值  清空操作;

例子

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;

返回值  不匹配的話返回 -1;  索引以0 開始

        nStar 代表以索引值nStart 的字符開始搜索 ,

即為包含以索引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;

返回值  不匹配的話返回 -1;  索引以0 開始

          注意::返回此字符串中第一個在lpszCharSet中 也包括字符並且從零開始的索引值

例子

CString s( "abcdef" );

ASSERT( s.FindOneOf( "xd" ) == 3 ); // 'd' is first match.

 

CString::Format

void Format( LPCTSTR lpszFormat, ... );

void Format( UINT nFormatID, ... );

lpszFormat  一個格式控制字符串

nFormatID  字符串標識符

例子

             CString str;

Str.Format(“%d”,13); 

此時Str為13

 

CString::GetAt

TCHAR GetAt( int nIndex ) const;

返回標號為nIndex的字符,你可以把字符串理解為一個數組,GetAt類似於[].注意nIndex的范圍,如果不合適會有調試錯誤。

 

CString::GetBuffer

LPTSTR GetBuffer( int nMinBufLength );

返回值

一個指向對象的(以空字符結尾的)字符緩沖區的LPTSTR 指針。

參數

nMinBufLength

字符緩沖區的以字符數表示的最小容量。這個值不包括一個結尾的空字符的空間。

說明

此成員函數返回一個指向CString 對象的內部字符緩沖區的指針。返回的LPTSTR 不是const,因此可以允許直接修改CString 的內容。如果你使用由GetBuffer 返回的指針來改變字符串的內容,你必須在使用其它的CString 成員函數之前調用ReleaseBuffer 函數。

在調用ReleaseBuffer 之后,由GetBuffer 返回的地址也許就無效了,因為其它的CString 操作可能會導致CString 緩沖區被重新分配。如果你沒有改變此CString 的長度,則緩沖區不會被重新分配。當此CString 對象被銷毀時,其緩沖區內存將被自動釋放。

注意,如果你自己知道字符串的長度,則你不應該添加結尾的空字符。但是,當你用ReleaseBuffer 來釋放該緩沖區時,你必須指定最后的字符串長度。如果你添加了結尾的空字符, 你應該給ReleaseBuffer 的長度參數傳遞-1 ,ReleaseBuffer 將對該緩沖區執行strlen 來確定它的長度。

下面的例子說明了如何用CString::GetBuffer。

// CString::GetBuffer 例子

CString s( "abcd" );

#ifdef _DEBUG

afxDump << "CString s " << s << "/n";

#endif

LPTSTR p = s.GetBuffer( 10 );

strcpy( p, "Hello" ); // 直接訪問CString 對象。

s.ReleaseBuffer( );

#ifdef _DEBUG

afxDump << "CString s " << s << "/n";

#endif

 

CString::GetLength

int GetLength( ) const;

返回值

返回字符串中的字節計數。

說明

此成員函數用來獲取這個CString 對象中的字節計數。這個計數不包括結尾的空字符。

對於多字節字符集(MBCS),GetLength 按每一個8 位字符計數;即,在一個多字節字符中的開始和結尾字節被算作兩個字節。

示例

下面的例子說明了如何使用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 );

返回修改后的長度,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!

 

CString::IsEmpty

BOOL IsEmpty( ) const;

返回值

如果CString 對象的長度為0,則返回非零值;否則返回0。

說明

此成員函數用來測試一個CString 對象是否是空的。

示例

下面的例子說明了如何使用CString::IsEmpty。

// CString::IsEmpty 示例

CString s;

ASSERT( s.IsEmpty() );

請參閱 CString::GetLength

 

CString::Left

CString Left( int nCount ) const; 

throw( CMemoryException );

返回的字符串是前nCount個字符。

例子

CString s( _T("abcdef") );

ASSERT( s.Left(2) == _T("ab") );

 

CString::LoadString

BOOL LoadString( UINT nID );

throw( CMemoryException );

返回值

如果加載資源成功則返回非零值;否則返回0。

nID  一個Windows 字符串資源ID。

說明 此成員函數用來讀取一個由nID 標識的Windows 字符串資源,並放入一個已有CString 對象中。

示例

下面的例子說明了如何使用CString::LoadString。

// CString::LoadString 示例

#define IDS_FILENOTFOUND 1

CString s;

if (! s.LoadString( IDS_FILENOTFOUND ))

 

CString::MakeLower

void MakeLower( );

改變字符的小寫

 

CString::MakeReverse

void MakeReverse( );

字符倒置

 

CString::MakeUpper

void MakeUpper( );

改變字符的大寫

CString::Mid
CString Mid( int nFirst ) const;  
CString Mid( int nFirst, int nCount ) const;  
nCount代表要提取的字符數, nFirst代表要提取的開始索引位置

例子
CString s( _T("abcdef") );
ASSERT( s.Mid( 2, 3 ) == _T("cde") );

CString::ReleaseBuffer

void ReleaseBuffer( int nNewLength = -1 );

參數

nNewLength

此字符串的以字符數表示的新長度,不計算結尾的空字符。如果這個字

符串是以空字符結尾的,則參數的缺省值-1 將把CString 的大小設置為

字符串的當前長度。

說明

使用ReleaseBuffer 來結束對由GetBuffer 分配的緩沖區的使用。如果你知道緩

沖區中的字符串是以空字符結尾的,則可以省略nNewLength 參數。如果字符

串不是以空字符結尾的,則可以使用nNewLength 指定字符串的長度。在調用

ReleaseBuffer 或其它CString 操作之后,由GetBuffer 返回的地址是無效的。

示例

下面的例子說明了如何使用CString::ReleaseBuffer。

// CString::ReleaseBuffer 示例

CString s;

s = "abc";

LPTSTR p = s.GetBuffer( 1024 );

strcpy(p, "abc"); // 直接使用該緩沖區

ASSERT( s.GetLength() == 3 ); // 字符串長度 = 3

s.ReleaseBuffer(); // 釋放多余的內存,現在p 無效。

ASSERT( s.GetLength() == 3 ); // 長度仍然是3

 

CString::Remove

int CString::Remove ( TCHAR ch );

返回值

返回從字符串中移走的字符數。如果字符串沒有改變則返回零。

參數

ch

要從一個字符串中移走的字符。

說明

此成員函數用來將ch 實例從字符串中移走。與這個字符的比較是區分大小寫

的。

示例

// 從一個句子中移走小寫字母'c':

CString str (“This is a test.”);

int n = str.Remove( 't' );

ASSERT( n == 2 );

ASSERT( str ==“This is a es. ” );

 

CString::Replace

int Replace( TCHAR chOld, TCHAR chNew );

int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );

返回值

返回被替換的字符數。如果這個字符串沒有改變則返回零。

參數

chOld

要被chNew 替換的字符。

chNew

要用來替換chOld 的字符。

lpszOld

一個指向字符串的指針,該字符串包含了要被lpszNew 替換的字符。

lpszNew

一個指向字符串的指針,該字符串包含了要用來替換lpszOld 的字符。

說明

此成員函數用一個字符替換另一個字符。函數的第一個原形在字符串中用chNew

現場替換chOld。函數的第二個原形用lpszNew 指定的字符串替換lpszOld 指定

的子串。

在替換之后,該字符串有可能增長或縮短;那是因為lpszNew lpszOld 的長度

不需要是相等的。兩種版本形式都進行區分大小寫的匹配。

示例

// 第一個例子,old 和new 具有相同的長度。

CString strZap( “C - -” );

int n = strZap.Replace('-', '+' );

ASSERT( n == 2 );

ASSERT(strZap == “C++” );

// 第二個例子,old 和new 具有不同的長度。

CString strBang( “Everybody likes ice hockey” );

n = strBang.Replace( “hockey”, “golf” );

ASSERT( n ==1 );

n = strBang.Replace ( “likes” , “plays” );

ASSERT( n == 1 );

n = strBang.Replace( “ice”, NULL );

ASSERT( n == 1 );

ASSERT( strBang == “Everybody plays golg” );

// 注意,現在在你的句子中有了一個額外的空格。

// 要移走這個額外的空格,可以將它包括在要被替換的字符串中,例如,“ice ”。

 

CString::ReverseFind

int ReverseFind( TCHAR ch ) const;

返回值

返回此CString 對象中與要求的字符匹配的最后一個字符的索引;如果沒有找

到需要的字符則返回-1。

參數

ch

要搜索的字符。

說明

此成員函數在此CString 對象中搜索與一個子串匹配的最后一個字符。此函數

類似於運行時函數strrchr。

示例

// CString::ReverseFind 示例

CString s( "abcabc" );

ASSERT( s.ReverseFind( 'b' ) == 4 );

 

CString::Right

CString Right( int nCount ) const; 

throw( CMemoryException );

返回的字符串是最后nCount個字符。

CString s( _T("abcdef") );

ASSERT( s.Right(2) == _T("ef") );

 

CString:: SetAt

void SetAt( int nIndex, TCHAR ch );

 

你可以把字符串理解為一個數組,SetAt類似於[].注意nIndex的范圍,如果不合適會有調試錯誤。 Ch 更替字符, 把nIndex位置上的字符 變成ch

 

CString s( "abc" );

s.MakeReverse();

ASSERT( s == "cba" );

 

 

CString::TrimLeft

void TrimLeft( );

void CString::TrimLeft( TCHAR chTarget );

如果沒有參數,從左刪除字符(/n/t空格等),至到遇到一個非此類字符. 當然你也可以指定刪除那些字符. 如果指定的參數是字符串,那么遇上其中的一個字符就刪除.
/n  換行符 
/t  TAB字符
CString str = "/n/t a";

str.TrimLeft();

str為“a”;

 

CString str = "abbcadbabcadb ";

str.TrimLeft("ab");

結果"cadbabcadb "

str.TrimLeft("ac");

結果"bcadbabcadb "

 

CString::TrimRight

void TrimRight( );

void CString::TrimRight( TCHAR chTarget );

void CString::TrimRight( LPCTSTR lpszTargets );

同上


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM