日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CString类所有成员函数详解

發(fā)布時間:2023/12/29 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CString类所有成员函数详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

VC里CString是我們最常用的類之一,我們覺得對它很熟悉了,可是你知道它的所有用法嗎?
還是系統(tǒng)的學(xué)習(xí)一下吧,認真看完本文就OK了。
下面開始:
CString::Compare
int Compare( LPCTSTR lpsz ) const;
返回值 字符串一樣 返回0
???????? 小于lpsz 返回-1
???????? 大于lpsz 返回1
???????? 區(qū)分大小字符
???????? 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
??????? 不區(qū)分大小字符
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是一次刪除幾個字符。根據(jù)我實驗得出的結(jié)果:當nCount>要刪除字符串的最大長度(GetCount() - nIndex)時會出錯,當nCount過大,沒有足夠的字符刪除時,此函數(shù)不執(zhí)行。
例子
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)就會執(zhí)行錯誤
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的字符,你可以把字符串理解為一個數(shù)組,GetAt類似于[].注意nIndex的范圍,如果不合適會有調(diào)試錯誤。
CString::GetBuffer
LPTSTR GetBuffer( int nMinBufLength );
返回值
一個指向?qū)ο蟮?#xff08;以空字符結(jié)尾的)字符緩沖區(qū)的LPTSTR 指針。
參數(shù)
nMinBufLength
字符緩沖區(qū)的以字符數(shù)表示的最小容量。這個值不包括一個結(jié)尾的空字符的空間。
說明
此成員函數(shù)返回一個指向CString 對象的內(nèi)部字符緩沖區(qū)的指針。返回的LPTSTR 不是const,因此可以允許直接修改CString 的內(nèi)容。如果你使用由GetBuffer 返回的指針來改變字符串的內(nèi)容,你必須在使用其它的CString 成員函數(shù)之前調(diào)用ReleaseBuffer 函數(shù)。
在調(diào)用ReleaseBuffer 之后,由GetBuffer 返回的地址也許就無效了,因為其它的CString 操作可能會導(dǎo)致CString 緩沖區(qū)被重新分配。如果你沒有改變此CString 的長度,則緩沖區(qū)不會被重新分配。當此CString 對象被銷毀時,其緩沖區(qū)內(nèi)存將被自動釋放。
注意,如果你自己知道字符串的長度,則你不應(yīng)該添加結(jié)尾的空字符。但是,當你用ReleaseBuffer 來釋放該緩沖區(qū)時,你必須指定最后的字符串長度。如果你添加了結(jié)尾的空字符, 你應(yīng)該給ReleaseBuffer 的長度參數(shù)傳遞-1 ,ReleaseBuffer 將對該緩沖區(qū)執(zhí)行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;
返回值
返回字符串中的字節(jié)計數(shù)。
說明
此成員函數(shù)用來獲取這個CString 對象中的字節(jié)計數(shù)。這個計數(shù)不包括結(jié)尾的空字符。
對于多字節(jié)字符集(MBCS),GetLength 按每一個8 位字符計數(shù);即,在一個多字節(jié)字符中的開始和結(jié)尾字節(jié)被算作兩個字節(jié)。
示例
下面的例子說明了如何使用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。
說明
此成員函數(shù)用來測試一個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。
說明 此成員函數(shù)用來讀取一個由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代表要提取的字符數(shù), nFirst代表要提取的開始索引位置
例子
CString s( _T("abcdef") );
ASSERT( s.Mid( 2, 3 ) == _T("cde") );
CString::ReleaseBuffer
void ReleaseBuffer( int nNewLength = -1 );
參數(shù)
nNewLength
此字符串的以字符數(shù)表示的新長度,不計算結(jié)尾的空字符。如果這個字
符串是以空字符結(jié)尾的,則參數(shù)的缺省值-1 將把CString 的大小設(shè)置為
字符串的當前長度。
說明
使用ReleaseBuffer 來結(jié)束對由GetBuffer 分配的緩沖區(qū)的使用。如果你知道緩
沖區(qū)中的字符串是以空字符結(jié)尾的,則可以省略nNewLength 參數(shù)。如果字符
串不是以空字符結(jié)尾的,則可以使用nNewLength 指定字符串的長度。在調(diào)用
ReleaseBuffer 或其它CString 操作之后,由GetBuffer 返回的地址是無效的。
示例
下面的例子說明了如何使用CString::ReleaseBuffer。
// CString::ReleaseBuffer 示例
CString s;
s = "abc";
LPTSTR p = s.GetBuffer( 1024 );
strcpy(p, "abc"); // 直接使用該緩沖區(qū)
ASSERT( s.GetLength() == 3 ); // 字符串長度 = 3
s.ReleaseBuffer(); // 釋放多余的內(nèi)存,現(xiàn)在p 無效。
ASSERT( s.GetLength() == 3 ); // 長度仍然是3
CString::Remove
int CString::Remove ( TCHAR ch );
返回值
返回從字符串中移走的字符數(shù)。如果字符串沒有改變則返回零。
參數(shù)
ch
要從一個字符串中移走的字符。
說明
此成員函數(shù)用來將ch 實例從字符串中移走。與這個字符的比較是區(qū)分大小寫
的。
示例
// 從一個句子中移走小寫字母'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 );
返回值
返回被替換的字符數(shù)。如果這個字符串沒有改變則返回零。
參數(shù)
chOld
要被chNew 替換的字符
chNew
要用來替換chOld 的字符。
lpszOld
一個指向字符串的指針,該字符串包含了要被lpszNew 替換的字符。
lpszNew
一個指向字符串的指針,該字符串包含了要用來替換lpszOld 的字符。
說明
此成員函數(shù)用一個字符替換另一個字符。函數(shù)的第一個原形在字符串中用chNew
現(xiàn)場替換chOld。函數(shù)的第二個原形用lpszNew 指定的字符串替換lpszOld 指定
的子串。
在替換之后,該字符串有可能增長或縮短;那是因為lpszNew 和lpszOld 的長度
不需要是相等的。兩種版本形式都進行區(qū)分大小寫的匹配。


CString類
CString類沒有基本類,類的聲明保存在afx.h頭文件中。
一個CString對象是由可變長度的字符串組成,CString類提供了函數(shù)和操作符,內(nèi)存管理,使用起來比字符數(shù)字容易的多。
CString類基于TCHAE數(shù)據(jù)類型,如果你的程序采用_UNICODE字符集,則TCHAR將被定義為16位,char類型默認采用8位
CString類的初始化:
CString();
聲明一個CString對象,但沒有初始化該對象。

CString(const CString& stringSrc);
Throw(CMemoryException);
該構(gòu)造函數(shù)表示將一個已經(jīng)存在的CString對象拷貝到聲明的CString對象中。

CString(TCHAR ch, int nRepeat = 1);
throw(CMemoryException);
該構(gòu)造函數(shù)表示將ch這個簡單字符重復(fù)nRepeat遍來創(chuàng)建一個CString對象。

CString(LPCTSTR lpch , int nLength);
throw(CMemoryException);
構(gòu)造一個長度為nLength的CString對象,該對象初始化值為一個字符串數(shù)組常量指針。

CString(const unsigned char* psz);
Throw(CMemoryException);
根據(jù)無符號字符指針創(chuàng)建CString對象。

CString(LPCWSTR lpsz);
throw(CMemoryException);
根據(jù)一個unicode字符串來創(chuàng)建一個CString對象。

CString(LPCSTR lpsz);
throw(CMemoryException);
根據(jù)一個ansi字符串來創(chuàng)建一個unicode CString對象。

作為數(shù)組的串:
int GetLength() const;
返回該串的字節(jié)數(shù),該函數(shù)返回值不包括空結(jié)束符”/0”,如果該CString對象被聲明成unicode字符集,則會按照一個字符兩個字節(jié)來計算CString對象的長度。

BOOL IsEmpty() const;
如果CString對象的長度為0則返回為非0,否則返回0值。

void Empty();
將CString對象設(shè)置為空串并且相應(yīng)的釋放掉內(nèi)存。

TCHAR GetAt(int nIndex) const;
返回該串特定位置的字符,nIndex是數(shù)組的下標,從0開始,注意nIndex的范圍為0到GetLength()-1。
TCHAR:
是為了統(tǒng)一多語言編碼而設(shè)計的;
ANSI-單字符編碼
?????????? UNICODE-雙字節(jié)字符編碼
?????????? UTF-8-三字節(jié)字符編碼

TCHAR operator[](int nIndex) const;
用數(shù)組下標的方式來返回某個字符,nIndex是數(shù)組的下標,從0開始,注意nIndex的范圍為0到GetLength()-1。注意你不能通過這種方式來改變該位置的字符。

void SetAt(int nIndex, TCHAR ch);
將CSring對象中nIndex位置的字符改為ch,注意nIndex的范圍為0到GetLength()-1,注意改函數(shù)只能修改某個位置上的字符,不能增加字符。

operator LPCTSTR() const;
返回串數(shù)據(jù)的字符指針

串合并操作:
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; 區(qū)別大小寫字幕
該函數(shù)對兩個CString對象進行,如果內(nèi)容完全一致則返回0;如果長度小于lpsz,則返回為-1,如果長度一致,但內(nèi)容不同,則返回-1;如果長度大于lpsz,則返回1。

int CompareNoCase(LPCTSTR lpsz) const; 不區(qū)分大小寫字母
該函數(shù)對兩個CString對象進行,如果內(nèi)容完全一致則返回0;如果長度小于lpsz,則返回為-1,如果長度一致,但內(nèi)容不同,則返回-1;如果長度大于lpsz,則返回1。

int Compare(LPCTSTR lpsz) const; 區(qū)別大小寫字幕
該函數(shù)對兩個CString對象進行,如果內(nèi)容完全一致則返回0;如果長度小于lpsz,則返回為-1,如果長度一致,但內(nèi)容不同,則返回-1;如果長度大于lpsz,則返回1。

int CompareNoCase(LPCTSTR lpsz) const; 不區(qū)分大小寫字母
該函數(shù)對兩個CString對象進行,如果內(nèi)容完全一致則返回0;如果長度小于lpsz,則返回為-1,如果長度一致,但內(nèi)容不同,則返回-1;如果長度大于lpsz,則返回1。

int Collate(LPCTSTR lpsz) const; 區(qū)別大小寫字幕
該函數(shù)對兩個CString對象進行,如果內(nèi)容完全一致則返回0;如果長度小于lpsz,則返回為-1,如果長度一致,但內(nèi)容不同,則返回-1;如果長度大于lpsz,則返回1。

int CollateNoCase(LPCTSTR lpsz) const; 不區(qū)分大小寫字母
該函數(shù)對兩個CString對象進行,如果內(nèi)容完全一致則返回0;如果長度小于lpsz,則返回為-1,如果長度一致,但內(nèi)容不同,則返回-1;如果長度大于lpsz,則返回1。

Compare和Collate 函數(shù)的區(qū)別:
Compare函數(shù)和==運算符是等效的,Compare、==、CompareNoCase可識別MBCS和Unicode,Collate區(qū)別區(qū)域性,運算比Compare慢,僅當有必要遵守當前區(qū)域指定的排序規(guī)則時,才使用Collate函數(shù)。
抽取函數(shù):
CString Mid(int nFirst) const;
throw(CMemoryException);
從一個CString對象中提取從nFirst開始到結(jié)尾的串,返回值是CString對象,注意返回值可能為空。

CString Mid(int nFirst , int nCount) const;
throw(CMemoryException);
從一個CString對象中提取從nFirst開始的長度為nCount的串,返回值是CString對象,注意返回值可能為空。
注意上述兩種用法,當CString對象是表示多字節(jié)的字符串時,nCount表示的是一個8為字符的長度,例,對應(yīng)unicode字符集要取n個字符則需要將nCount=2*n。

CString Left(int nCount) const;
throw(CMemoryException);
從一個CString對象中提取從開始位置,長度為nCount的串,返回值是CString對象,注意返回值可能為空。

CString Right(int nCount) const;
throw(CMemoryException);
從一個CString對象中提取從結(jié)束位置,長度為nCount的串,返回值是CString對象,注意返回值可能為空。

CString SpanIncluding(LPCTSTR lpszCharSet) const;
throw(CMemoryException);
從原CString對象中第一個字符開始提取字符charactor與指定的LPCTSTR傳進行比對,如果該字符charactor在LPCTSTR串中,則繼續(xù)進行下一個字符是否在LPCTSTR串中的比較,如果不在,則將該字符前的所有字符串返回構(gòu)造成一個新的CString對象,如果從第一開始就不在LPCTSTR串中,則返回一個空CString對象。

CString SpanExcluding(LPCTSTR lpszCharSet) const;
throw(CMemoryException);
作用與SpanIncluding函數(shù)想反,表示從CString對象中一旦找到是LPCTSTR串中字符時就返回一個CString對象。

轉(zhuǎn)換函數(shù):
void MakeUpper();
將CString對象里的字符全部轉(zhuǎn)換為大寫字符。

void MakeLower();
將CString對象里的字符全部轉(zhuǎn)換為小寫字符。

void MakeReverse();
將CString對象里的字符串顛倒順序。

int Replace(TCHAR chOld, TCHAR chNew);
int Replace(LPCTSTR lpszOld , LPCTSTR lpszNew);
該函數(shù)返回值表示被替換的chOld、lpszOld的個數(shù)。

int Remove(TCHAR ch);
從一個CString對象中刪除ch字符串,返回值是刪除的ch字符串個數(shù),如果該對象沒有改變,表示沒有ch字符串被刪除,返回值為0。

int Insert(int nIndex, TCHAR ch);
throw(CMemoryException);
int Insert(int nIndex, LPCTSTR pstr);
throw(CMemoryException);
向CString對象插入一個字符或者字符串,從nIndex位置開始開始插入,如果nIndex為0,表示從首部開始,如果nIndex大于CString對象的長度,則會從尾部開始。該函數(shù)返回改變長度后的對象的長度。

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詳細內(nèi)容參照:
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 有符號的十進制整數(shù)
i int 有符號的十進制整數(shù)
o int 無符合的八進制整數(shù)
u int 無符合的八進制整數(shù)
x int 無符號的十六進制整數(shù), using "abcdef."
X int 無符號的十六進制整數(shù), 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);
將函數(shù)Format中關(guān)于…部分的內(nèi)容用va_list變量來表示,功能和Format相同。
void TrimLeft();
void CString::TrimLeft(TCHAR chTarget);
void CString::TrimLeft(LPCTSTR lpszTargets);
當該函數(shù)沒有參數(shù)時,對應(yīng)一個CString對象,將去掉串的首部空白空格(新行、空格、tab字符)字符串。
當該函數(shù)有參數(shù)時,將從CString對象的開始去掉規(guī)定的字符TCHAR或字符串lpszTargets。

void TrimRight();
void CString::TrimRight(TCHAR chTarget);
void CString::TrimRight(LPCTSTR lpszTargets);
用法與TrimLeft相似,不過是處理CString對象的尾部字符。

void FormatMessage(LPCTSTR lpszFormat,…);
void FormatMessage(UINT nFormatID,…);
格式化消息字符串,

查找函數(shù):
int Find(TCHAR ch) const;
int Find(LPCTSTR lpszSub) const;
int Find(TCHAR ch , int nStart) const;
int Find(LPCTSTR lpszSub , int nStart) const;
該函數(shù)用來在CString對象中查找字符ch或者字符串lpszSub,如果找到則返回第一個字符所在位置索引值。nStart表示該函數(shù)可以從規(guī)定的位置開始進行查找。

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對象內(nèi)容寫入到CArchive中。
CArchiev>>CString:將CArchive對象內(nèi)容寫入到CString中。

緩沖區(qū)訪問函數(shù):
LPTSTR GetBuffer(int nMinBufLength);
throw(CMemoryException);
返回CString對象的內(nèi)容緩沖區(qū)的頭指針,nMinBufLength是設(shè)定的內(nèi)容緩沖區(qū)的大小,在實際的使用中采用
CString::GetBuffer(CString::GetLength())或者用CString::GetBuffer(0)
通過這個方法可以修改對象的內(nèi)容。

void ReleaseBuffer(int nNewLength = -1);
按照nNewLength值的大小來最終確定CString對象的內(nèi)容緩沖區(qū)的長度,更新CString對象內(nèi)部的信息,這樣才能保證下面的對象的其它操作。當使用ReleaseBuffer()或者ReleaseBuffer(-1)時,表示按照CString對象內(nèi)容實際的大小來設(shè)定,會將GetBuffer()聲明的多余的空間釋放掉,如果ReleaseBuffer(nNewlength >0)時,則CString對象會按照nNewLength設(shè)定的值來調(diào)整CString內(nèi)容。

LPTSTR GetBufferSetLength(int nNewLength);
throw(CMemoryException);
返回CString對象的內(nèi)容緩沖區(qū)的頭指針,nNewLength是設(shè)定的內(nèi)容緩沖區(qū)的大小


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

說明 調(diào)用GetBuffer(100)之前,str對象的長度是15

當調(diào)用GetBuffer(100)時,跟蹤str對象的長度還是15

但是str的內(nèi)容更新為” hellohellohellohello”(實際的長度為20)

調(diào)用ReleaseBuffer()后,str的長度為20 調(diào)用GetBuffer(100)之前,str對象的長度是15

當調(diào)用GetBufferSetLength(100)時,跟蹤str對象的長度是100

但是str的內(nèi)容更新為” hellohellohellohello”(實際的長度為20)

調(diào)用ReleaseBuffer()后,str的長度為20
void FreeExtra();
釋放CString對象之前聲明的不在使用的多余的內(nèi)存空間,
LPTSTR LockBuffer();
LPTSTR UnLockBuffer();
這對函數(shù)表示對引用的內(nèi)存塊進行加鎖解鎖。調(diào)用LockBuffer(),它創(chuàng)建了一個字符串副本,并設(shè)置CString對象的引用計數(shù)器為-1,保護起內(nèi)存區(qū)。當字符串內(nèi)存區(qū)處于鎖狀態(tài)時,通過兩種方法來保護串內(nèi)容不被修改:
用于Windows的特殊函數(shù):
BSTR AllocSysString() const;
throw(CMemoryException);
返回一個重新分配空間的串的指針。該函數(shù)將一個字符串轉(zhuǎn)換為unicode字符集形式,使用完成后要調(diào)用SysFreeString()函數(shù)釋放字符串。使用時如果你使用MFC共享動態(tài)庫并在debug模式下創(chuàng)建應(yīng)用程序則必須鏈接MFC042D.DLL,在申明的stdafx.h的頭文件中包含afxdisp.h頭文件。
?

?

總結(jié)

以上是生活随笔為你收集整理的CString类所有成员函数详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。