CString类所有成员函数详解
VC里CString是我們最常用的類之一,我們覺得對它很熟悉了,可是你知道它的所有用法嗎?
還是系統的學習一下吧,認真看完本文就OK了。
下面開始:
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::Right
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 的長度
不需要是相等的。兩種版本形式都進行區分大小寫的匹配。
CString類
CString類沒有基本類,類的聲明保存在afx.h頭文件中。
一個CString對象是由可變長度的字符串組成,CString類提供了函數和操作符,內存管理,使用起來比字符數字容易的多。
CString類基于TCHAE數據類型,如果你的程序采用_UNICODE字符集,則TCHAR將被定義為16位,char類型默認采用8位
CString類的初始化:
CString();
聲明一個CString對象,但沒有初始化該對象。
CString(const CString& stringSrc);
Throw(CMemoryException);
該構造函數表示將一個已經存在的CString對象拷貝到聲明的CString對象中。
CString(TCHAR ch, int nRepeat = 1);
throw(CMemoryException);
該構造函數表示將ch這個簡單字符重復nRepeat遍來創建一個CString對象。
CString(LPCTSTR lpch , int nLength);
throw(CMemoryException);
構造一個長度為nLength的CString對象,該對象初始化值為一個字符串數組常量指針。
CString(const unsigned char* psz);
Throw(CMemoryException);
根據無符號字符指針創建CString對象。
CString(LPCWSTR lpsz);
throw(CMemoryException);
根據一個unicode字符串來創建一個CString對象。
CString(LPCSTR lpsz);
throw(CMemoryException);
根據一個ansi字符串來創建一個unicode CString對象。
作為數組的串:
int GetLength() const;
返回該串的字節數,該函數返回值不包括空結束符”/0”,如果該CString對象被聲明成unicode字符集,則會按照一個字符兩個字節來計算CString對象的長度。
BOOL IsEmpty() const;
如果CString對象的長度為0則返回為非0,否則返回0值。
void Empty();
將CString對象設置為空串并且相應的釋放掉內存。
TCHAR GetAt(int nIndex) const;
返回該串特定位置的字符,nIndex是數組的下標,從0開始,注意nIndex的范圍為0到GetLength()-1。
TCHAR:
是為了統一多語言編碼而設計的;
ANSI-單字符編碼
?????????? UNICODE-雙字節字符編碼
?????????? UTF-8-三字節字符編碼
TCHAR operator[](int nIndex) const;
用數組下標的方式來返回某個字符,nIndex是數組的下標,從0開始,注意nIndex的范圍為0到GetLength()-1。注意你不能通過這種方式來改變該位置的字符。
void SetAt(int nIndex, TCHAR ch);
將CSring對象中nIndex位置的字符改為ch,注意nIndex的范圍為0到GetLength()-1,注意改函數只能修改某個位置上的字符,不能增加字符。
operator LPCTSTR() const;
返回串數據的字符指針
串合并操作:
const CString& operator = (const CString& stringSrc);
throw(CMemoryException);
const CString& operator = (TCHAR ch);
throw(CMemoryException);
const CString& operator = (const unsigned char* psz);
throw(CMemoryException);
const CString& operator = (LPCWSTR lpsz);
throw(CMemoryException);
const CString& operator = (LPCSTR lpsz);
throw(CMemoryException);
賦值操作,不考慮對象的長度。
friend CString operator + (const CString& string1, const CString& string2);
throw(CMmeoryException);
friend CString operator + (const CString& string, TCHAR ch);
throw(CMmeoryException);
friend CString operator + (TCHAR ch, const CString& string);
throw(CMmeoryException);
friend CString operator + (const CString& string, LPCTSTR lpsz);
throw(CMmeoryException);
friend CString operator + (LPCTSTR lpsz, const CString& string);
throw(CMmeoryException);
const CString& operator += (const CString& string);
throw(CMemoryException);
const CString& operator += (TCHAR ch);
throw(CMemoryException);
const CString& operator += (LPCTSTR lpsz);
throw(CMemoryException);
比較操作符:只能使用兩種CString , LPCTSTR
BOOL operator ==(const CString& s1, const CString& s2);
BOOL operator ==(const CString& s1, LPCTSTR s2);
BOOL operator ==( LPCTSTR s1, const CString& s2);
BOOL operator !=(const CString& s1, const CString& s2);
BOOL operator !=(const CString& s1, LPCTSTR s2);
BOOL operator !=( LPCTSTR s1, const CString& s2);
BOOL operator <=(const CString& s1, const CString& s2);
BOOL operator <=(const CString& s1, LPCTSTR s2);
BOOL operator <=( LPCTSTR s1, const CString& s2);
BOOL operator >=(const CString& s1, const CString& s2);
BOOL operator >=(const CString& s1, LPCTSTR s2);
BOOL operator >=( LPCTSTR s1, const CString& s2);
BOOL operator <(const CString& s1, const CString& s2);
BOOL operator <(const CString& s1, LPCTSTR s2);
BOOL operator <( LPCTSTR s1, const CString& s2);
BOOL operator >(const CString& s1, const CString& s2);
BOOL operator >(const CString& s1, LPCTSTR s2);
BOOL operator >( LPCTSTR s1, const CString& s2);
int Compare(LPCTSTR lpsz) const; 區別大小寫字幕
該函數對兩個CString對象進行,如果內容完全一致則返回0;如果長度小于lpsz,則返回為-1,如果長度一致,但內容不同,則返回-1;如果長度大于lpsz,則返回1。
int CompareNoCase(LPCTSTR lpsz) const; 不區分大小寫字母
該函數對兩個CString對象進行,如果內容完全一致則返回0;如果長度小于lpsz,則返回為-1,如果長度一致,但內容不同,則返回-1;如果長度大于lpsz,則返回1。
int Compare(LPCTSTR lpsz) const; 區別大小寫字幕
該函數對兩個CString對象進行,如果內容完全一致則返回0;如果長度小于lpsz,則返回為-1,如果長度一致,但內容不同,則返回-1;如果長度大于lpsz,則返回1。
int CompareNoCase(LPCTSTR lpsz) const; 不區分大小寫字母
該函數對兩個CString對象進行,如果內容完全一致則返回0;如果長度小于lpsz,則返回為-1,如果長度一致,但內容不同,則返回-1;如果長度大于lpsz,則返回1。
int Collate(LPCTSTR lpsz) const; 區別大小寫字幕
該函數對兩個CString對象進行,如果內容完全一致則返回0;如果長度小于lpsz,則返回為-1,如果長度一致,但內容不同,則返回-1;如果長度大于lpsz,則返回1。
int CollateNoCase(LPCTSTR lpsz) const; 不區分大小寫字母
該函數對兩個CString對象進行,如果內容完全一致則返回0;如果長度小于lpsz,則返回為-1,如果長度一致,但內容不同,則返回-1;如果長度大于lpsz,則返回1。
Compare和Collate 函數的區別:
Compare函數和==運算符是等效的,Compare、==、CompareNoCase可識別MBCS和Unicode,Collate區別區域性,運算比Compare慢,僅當有必要遵守當前區域指定的排序規則時,才使用Collate函數。
抽取函數:
CString Mid(int nFirst) const;
throw(CMemoryException);
從一個CString對象中提取從nFirst開始到結尾的串,返回值是CString對象,注意返回值可能為空。
CString Mid(int nFirst , int nCount) const;
throw(CMemoryException);
從一個CString對象中提取從nFirst開始的長度為nCount的串,返回值是CString對象,注意返回值可能為空。
注意上述兩種用法,當CString對象是表示多字節的字符串時,nCount表示的是一個8為字符的長度,例,對應unicode字符集要取n個字符則需要將nCount=2*n。
CString Left(int nCount) const;
throw(CMemoryException);
從一個CString對象中提取從開始位置,長度為nCount的串,返回值是CString對象,注意返回值可能為空。
CString Right(int nCount) const;
throw(CMemoryException);
從一個CString對象中提取從結束位置,長度為nCount的串,返回值是CString對象,注意返回值可能為空。
CString SpanIncluding(LPCTSTR lpszCharSet) const;
throw(CMemoryException);
從原CString對象中第一個字符開始提取字符charactor與指定的LPCTSTR傳進行比對,如果該字符charactor在LPCTSTR串中,則繼續進行下一個字符是否在LPCTSTR串中的比較,如果不在,則將該字符前的所有字符串返回構造成一個新的CString對象,如果從第一開始就不在LPCTSTR串中,則返回一個空CString對象。
CString SpanExcluding(LPCTSTR lpszCharSet) const;
throw(CMemoryException);
作用與SpanIncluding函數想反,表示從CString對象中一旦找到是LPCTSTR串中字符時就返回一個CString對象。
轉換函數:
void MakeUpper();
將CString對象里的字符全部轉換為大寫字符。
void MakeLower();
將CString對象里的字符全部轉換為小寫字符。
void MakeReverse();
將CString對象里的字符串顛倒順序。
int Replace(TCHAR chOld, TCHAR chNew);
int Replace(LPCTSTR lpszOld , LPCTSTR lpszNew);
該函數返回值表示被替換的chOld、lpszOld的個數。
int Remove(TCHAR ch);
從一個CString對象中刪除ch字符串,返回值是刪除的ch字符串個數,如果該對象沒有改變,表示沒有ch字符串被刪除,返回值為0。
int Insert(int nIndex, TCHAR ch);
throw(CMemoryException);
int Insert(int nIndex, LPCTSTR pstr);
throw(CMemoryException);
向CString對象插入一個字符或者字符串,從nIndex位置開始開始插入,如果nIndex為0,表示從首部開始,如果nIndex大于CString對象的長度,則會從尾部開始。該函數返回改變長度后的對象的長度。
int Delete(int nIndex, int nCount = 1);
throw(CMemoryException);
對CString對象進行字符或者字符串進行刪除操作,從nIndex位置開始的nCount個字符從CString對象中刪除,返回值為操作后CString對象的長度。
void Format(LPCTSTR lpszFormat,…);
void Format(UINT nFormatID,…);
將CString對象值按照lpszFormat或者nFormatID定義的格式進行了修改。
CString?? str = "大夫拉薩地方";??
int?? i=10;??
str.Format("%i",i);
str的最終值是"10"
lpszFormat詳細內容參照:
http://msdn2.microsoft.com/en-us/library/hf4y5e3w(VS.80).aspx
里的說明。
printf Type Field Characters
Character Type Output format
c int or wint_t When used with printf functions, 指定為8位字符; when used with wprintf functions, 指定位為16位字符。.
C int or wint_t When used with printf functions, 指定位為16位字符; when used with wprintf functions, 指定為8位字符.用法和c相反。
d int 有符號的十進制整數
i int 有符號的十進制整數
o int 無符合的八進制整數
u int 無符合的八進制整數
x int 無符號的十六進制整數, using "abcdef."
X int 無符號的十六進制整數, using "ABCDEF."
e double Signed value having the form [ – ]d.dddd e [sign]dd[d] where d is a single decimal digit, dddd is one or more decimal digits, dd[d] is two or three decimal digits depending on the output format and size of the exponent, and sign is + or –.
E double Identical to the e format except that E rather than e introduces the exponent.
f double Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision.
g double Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than –4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it.
G double Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate).
a double Signed hexadecimal double precision floating point value having the form [?]0xh.hhhh p±dd, where h.hhhh are the hex digits (using lower case letters) of the mantissa, and dd are one or more digits for the exponent. The precision specifies the number of digits after the point.
A double Signed hexadecimal double precision floating point value having the form [?]0Xh.hhhh P±dd, where h.hhhh are the hex digits (using capital letters) of the mantissa, and dd are one or more digits for the exponent. The precision specifies the number of digits after the point.
n Pointer to integer Number of characters successfully written so far to the stream or buffer; this value is stored in the integer whose address is given as the argument. See Security Note below.
p Pointer to void Prints the address of the argument in hexadecimal digits.
s String When used with printf functions, specifies a single-byte–character string; when used with wprintf functions, specifies a wide-character string. Characters are printed up to the first null character or until the precision value is reached.
S String When used with printf functions, specifies a wide-character string; when used with wprintf functions, specifies a single-byte–character string. Characters are printed up to the first null character or until the precision value is reached.
void FormatV(LPCTSTR lpszFormat,va_list argList);
將函數Format中關于…部分的內容用va_list變量來表示,功能和Format相同。
void TrimLeft();
void CString::TrimLeft(TCHAR chTarget);
void CString::TrimLeft(LPCTSTR lpszTargets);
當該函數沒有參數時,對應一個CString對象,將去掉串的首部空白空格(新行、空格、tab字符)字符串。
當該函數有參數時,將從CString對象的開始去掉規定的字符TCHAR或字符串lpszTargets。
void TrimRight();
void CString::TrimRight(TCHAR chTarget);
void CString::TrimRight(LPCTSTR lpszTargets);
用法與TrimLeft相似,不過是處理CString對象的尾部字符。
void FormatMessage(LPCTSTR lpszFormat,…);
void FormatMessage(UINT nFormatID,…);
格式化消息字符串,
查找函數:
int Find(TCHAR ch) const;
int Find(LPCTSTR lpszSub) const;
int Find(TCHAR ch , int nStart) const;
int Find(LPCTSTR lpszSub , int nStart) const;
該函數用來在CString對象中查找字符ch或者字符串lpszSub,如果找到則返回第一個字符所在位置索引值。nStart表示該函數可以從規定的位置開始進行查找。
int ReverseFind(TCHAR ch) const;
在CString對象中找到最后一個字符ch,返回值是該位置值,如果沒有找到返回-1。
int FindOneOf(LPCTSTR lpszCharSet) const;
從CString對象中查找到字符串lpszCharSet中任何一個字符就返回該位置值。
friend CArchive& operator <<(CArchive& ar, const CString& string);
throw(CMemoryException);
friend CArchive& operator >>(CArchive& ar, const CString& string);
throw(CMemoryException);
friend CDumpContext& operator <<(CDumpContext& dc, const CString& string);
throw(CMemoryException);
CArchiev<<CString:將CString對象內容寫入到CArchive中。
CArchiev>>CString:將CArchive對象內容寫入到CString中。
緩沖區訪問函數:
LPTSTR GetBuffer(int nMinBufLength);
throw(CMemoryException);
返回CString對象的內容緩沖區的頭指針,nMinBufLength是設定的內容緩沖區的大小,在實際的使用中采用
CString::GetBuffer(CString::GetLength())或者用CString::GetBuffer(0)
通過這個方法可以修改對象的內容。
void ReleaseBuffer(int nNewLength = -1);
按照nNewLength值的大小來最終確定CString對象的內容緩沖區的長度,更新CString對象內部的信息,這樣才能保證下面的對象的其它操作。當使用ReleaseBuffer()或者ReleaseBuffer(-1)時,表示按照CString對象內容實際的大小來設定,會將GetBuffer()聲明的多余的空間釋放掉,如果ReleaseBuffer(nNewlength >0)時,則CString對象會按照nNewLength設定的值來調整CString內容。
LPTSTR GetBufferSetLength(int nNewLength);
throw(CMemoryException);
返回CString對象的內容緩沖區的頭指針,nNewLength是設定的內容緩沖區的大小
?? GetBuffer(nMinBufLength) GetBufferSetLength(nNewLength)
代碼功能 CString str("ddddddddddddddd");
CString mm;
LPTSTR p;
int i;
p = str.GetBuffer(100);
strcpy(p,"hellohellohellohello");
str.ReleaseBuffer();
?? CString str("ddddddddddddddd");
CString mm;
LPTSTR p;
int i;
p = str. GetBufferSetLength(100);
strcpy(p,"hellohellohellohello");
str.ReleaseBuffer();
說明 調用GetBuffer(100)之前,str對象的長度是15
當調用GetBuffer(100)時,跟蹤str對象的長度還是15
但是str的內容更新為” hellohellohellohello”(實際的長度為20)
調用ReleaseBuffer()后,str的長度為20 調用GetBuffer(100)之前,str對象的長度是15
當調用GetBufferSetLength(100)時,跟蹤str對象的長度是100
但是str的內容更新為” hellohellohellohello”(實際的長度為20)
調用ReleaseBuffer()后,str的長度為20
void FreeExtra();
釋放CString對象之前聲明的不在使用的多余的內存空間,
LPTSTR LockBuffer();
LPTSTR UnLockBuffer();
這對函數表示對引用的內存塊進行加鎖解鎖。調用LockBuffer(),它創建了一個字符串副本,并設置CString對象的引用計數器為-1,保護起內存區。當字符串內存區處于鎖狀態時,通過兩種方法來保護串內容不被修改:
用于Windows的特殊函數:
BSTR AllocSysString() const;
throw(CMemoryException);
返回一個重新分配空間的串的指針。該函數將一個字符串轉換為unicode字符集形式,使用完成后要調用SysFreeString()函數釋放字符串。使用時如果你使用MFC共享動態庫并在debug模式下創建應用程序則必須鏈接MFC042D.DLL,在申明的stdafx.h的頭文件中包含afxdisp.h頭文件。
?
?
總結
以上是生活随笔為你收集整理的CString类所有成员函数详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Jplayer歌词同步显示插件
- 下一篇: Winamp 插件技术