CString类所有成员函数详解.doc

上传人:scccc 文档编号:12061396 上传时间:2021-12-01 格式:DOC 页数:19 大小:51.50KB
返回 下载 相关 举报
CString类所有成员函数详解.doc_第1页
第1页 / 共19页
CString类所有成员函数详解.doc_第2页
第2页 / 共19页
CString类所有成员函数详解.doc_第3页
第3页 / 共19页
CString类所有成员函数详解.doc_第4页
第4页 / 共19页
CString类所有成员函数详解.doc_第5页
第5页 / 共19页
点击查看更多>>
资源描述

《CString类所有成员函数详解.doc》由会员分享,可在线阅读,更多相关《CString类所有成员函数详解.doc(19页珍藏版)》请在三一文库上搜索。

1、CString类所有成员函数详解2009-03-27 10:53CString类所有成员函数详解推荐精选VC里CString是我们最常用的类之一,我们觉得对它很熟悉了,可是你知道它的所有用法吗?还是系统的学习一下吧,认真看完本文就OK了。下面开始:CString:Compareint Compare( LPCTSTR lpsz ) const;返回值 字符串一样 返回0         小于lpsz 返回-1         大于lps

2、z 返回1         区分大小字符         CString s1( "abc" );CString s2( "abd" );ASSERT( s1.Compare( s2 ) = -1 );ASSERT( s1.Compare( "abe" ) = -1 );CString:CompareNoCaseint CompareNoCase( LPCTSTR lpsz )

3、 const;返回值 字符串一样 返回0        小于lpsz 返回-1        大于lpsz 返回1        不区分大小字符CString:Collateint Collate( LPCTSTR lpsz ) const;同CString:CompareCString:CollateNoCaseint CollateNocase( LPCTSTR lpsz ) con

4、st;同CString:CompareNoCaseCString:CStringCString( );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;     &#

5、160;               CString s2( "cat" );              CString s3 = s2;              &#

6、160;  CString s4( s2 + " " + s3 );        CString s5( 'x' );                      / s5 = "x"CString s6( 'x', 6 ); 

7、                  / s6 = "xxxxxx"CString s7(LPCSTR)ID_FILE_NEW);        / s7 = "Create a new document"CString city = "Philadelphia"CString:Deleteint D

8、elete( 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);   

9、0;  str1.Delete(2,3);如果nCount(3) > GetCount() nIndex (5-2)就会执行错误CString:EmptyVoid Empty( );没有返回值 清空操作;例子CString s( "abc" );s.Empty();ASSERT( s.GetLength( ) = 0 );CString:Findint Find( TCHAR ch ) const;int Find( LPCTSTR lpszSub ) const;int Find( TCHAR ch, int nStart ) const;int Find

10、( 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”);In

11、g n = str.Find('e',5);ASSERT(n = 12)CString:FindOneOfint FindOneOf( LPCTSTR lpszCharSet ) const;返回值 不匹配的话返回 -1; 索引以0 开始          注意:返回此字符串中第一个在lpszCharSet中 也包括字符并且从零开始的索引值例子CString s( "abcdef" );ASSERT( s.FindOneOf( "xd" ) = 3

12、); / 'd' is first match.CString:Formatvoid Format( LPCTSTR lpszFormat, . );void Format( UINT nFormatID, . );lpszFormat 一个格式控制字符串nFormatID 字符串标识符例子             CString str;Str.Format(“%d”,13);此时Str为13CString:GetAtTCHAR GetAt( int nIn

13、dex ) const;返回标号为nIndex的字符,你可以把字符串理解为一个数组,GetAt类似于.注意nIndex的范围,如果不合适会有调试错误。CString:GetBufferLPTSTR GetBuffer( int nMinBufLength );返回值一个指向对象的(以空字符结尾的)字符缓冲区的LPTSTR 指针。参数nMinBufLength字符缓冲区的以字符数表示的最小容量。这个值不包括一个结尾的空字符的空间。说明此成员函数返回一个指向CString 对象的内部字符缓冲区的指针。返回的LPTSTR 不是const,因此可以允许直接修改CString 的内容。如果你使用由Get

14、Buffer 返回的指针来改变字符串的内容,你必须在使用其它的CString 成员函数之前调用ReleaseBuffer 函数。在调用ReleaseBuffer 之后,由GetBuffer 返回的地址也许就无效了,因为其它的CString 操作可能会导致CString 缓冲区被重新分配。如果你没有改变此CString 的长度,则缓冲区不会被重新分配。当此CString 对象被销毁时,其缓冲区内存将被自动释放。注意,如果你自己知道字符串的长度,则你不应该添加结尾的空字符。但是,当你用ReleaseBuffer 来释放该缓冲区时,你必须指定最后的字符串长度。如果你添加了结尾的空字符, 你应该给Re

15、leaseBuffer 的长度参数传递-1 ,ReleaseBuffer 将对该缓冲区执行strlen 来确定它的长度。下面的例子说明了如何用CString:GetBuffer。/ CString:GetBuffer 例子CString s( "abcd" );#ifdef _DEBUGafxDump << "CString s " << s << "n"#endifLPTSTR p = s.GetBuffer( 10 );strcpy( p, "Hello" ); / 直接访问

16、CString 对象。s.ReleaseBuffer( );#ifdef _DEBUGafxDump << "CString s " << s << "n"#endifCString:GetLengthint GetLength( ) const;返回值返回字符串中的字节计数。说明此成员函数用来获取这个CString 对象中的字节计数。这个计数不包括结尾的空字符。对于多字节字符集(MBCS),GetLength 按每一个8 位字符计数;即,在一个多字节字符中的开始和结尾字节被算作两个字节。示例下面的例子说明了如何使用C

17、String:GetLength。/ CString:GetLength 示例CString s( "abcdef" );ASSERT( s.GetLength() = 6 );CString:Insertint Insert( int nIndex, TCHAR ch );int Insert( int nIndex, LPCTSTR pstr );返回修改后的长度,nIndex是字符(或字符串)插入后的索引号例子CString str( “HockeyBest”);int n = str.Insert( 6, “is” );ASSERT( n = str.GetLeng

18、th( ) );printf( “1: %sn”, ( LPCTSTR ) str );n = str.Insert( 6, ' ' );ASSERT( n = str.GetLength( ) );printf ( “2: %sn”, (LPCTSTR) STR );n = str.Insert(555, 1);ASSERT( n = str.GetLength ( ) );printf ( “3: %sn”, ( LPCTSTR ) str );输出1. Hockeyis Best2. Hockey is Best3. Hockey is Best!CString:IsEm

19、ptyBOOL IsEmpty( ) const;返回值如果CString 对象的长度为0,则返回非零值;否则返回0。说明此成员函数用来测试一个CString 对象是否是空的。示例下面的例子说明了如何使用CString:IsEmpty。/ CString:IsEmpty 示例CString s;ASSERT( s.IsEmpty() );请参阅 CString:GetLengthCString:Left CString:RightCString Left( int nCount ) const;throw( CMemoryException );返回的字符串是前nCount个字符。例子CStr

20、ing s( _T("abcdef") );ASSERT( s.Left(2) = _T("ab") );CString:LoadStringBOOL LoadString( UINT nID );throw( CMemoryException );返回值如果加载资源成功则返回非零值;否则返回0。nID 一个Windows 字符串资源ID。说明 此成员函数用来读取一个由nID 标识的Windows 字符串资源,并放入一个已有CString 对象中。示例下面的例子说明了如何使用CString:LoadString。/ CString:LoadString

21、示例#define IDS_FILENOTFOUND 1CString s;if (! s.LoadString( IDS_FILENOTFOUND )CString:MakeLowervoid MakeLower( );改变字符的小写CString:MakeReversevoid MakeReverse( );字符倒置CString:MakeUppervoid MakeUpper( );改变字符的大写CString:MidCString Mid( int nFirst ) const;CString Mid( int nFirst, int nCount ) const;nCount代表要提

22、取的字符数, nFirst代表要提取的开始索引位置例子CString s( _T("abcdef") );ASSERT( s.Mid( 2, 3 ) = _T("cde") );CString:ReleaseBuffervoid ReleaseBuffer( int nNewLength = -1 );参数nNewLength此字符串的以字符数表示的新长度,不计算结尾的空字符。如果这个字符串是以空字符结尾的,则参数的缺省值1 将把CString 的大小设置为字符串的当前长度。说明使用ReleaseBuffer 来结束对由GetBuffer 分配的缓冲区的

23、使用。如果你知道缓冲区中的字符串是以空字符结尾的,则可以省略nNewLength 参数。如果字符串不是以空字符结尾的,则可以使用nNewLength 指定字符串的长度。在调用ReleaseBuffer 或其它CString 操作之后,由GetBuffer 返回的地址是无效的。示例下面的例子说明了如何使用CString:ReleaseBuffer。/ CString:ReleaseBuffer 示例CString s;s = "abc"LPTSTR p = s.GetBuffer( 1024 );strcpy(p, "abc"); / 直接使用该缓冲区AS

24、SERT( s.GetLength() = 3 ); / 字符串长度 = 3s.ReleaseBuffer(); / 释放多余的内存,现在p 无效。ASSERT( s.GetLength() = 3 ); / 长度仍然是3CString:Removeint CString:Remove ( TCHAR ch );返回值返回从字符串中移走的字符数。如果字符串没有改变则返回零。参数ch要从一个字符串中移走的字符。说明此成员函数用来将ch 实例从字符串中移走。与这个字符的比较是区分大小写的。示例/ 从一个句子中移走小写字母'c':CString str (“This is a tes

25、t.”);int n = str.Remove( 't' );ASSERT( n = 2 );ASSERT( str =“This is a es. ” );CString:Replaceint Replace( TCHAR chOld, TCHAR chNew );int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );返回值返回被替换的字符数。如果这个字符串没有改变则返回零。参数chOld要被chNew 替换的字符chNew要用来替换chOld 的字符。lpszOld一个指向字符串的指针,该字符串包含了要被lpszNew 替换的字符。

26、lpszNew一个指向字符串的指针,该字符串包含了要用来替换lpszOld 的字符。说明此成员函数用一个字符替换另一个字符。函数的第一个原形在字符串中用chNew现场替换chOld。函数的第二个原形用lpszNew 指定的字符串替换lpszOld 指定的子串。在替换之后,该字符串有可能增长或缩短;那是因为lpszNew 和lpszOld 的长度不需要是相等的。两种版本形式都进行区分大小写的匹配。推荐精选CString类CString类没有基本类,类的声明保存在afx.h头文件中。一个CString对象是由可变长度的字符串组成,CString类提供了函数和操作符,内存管理,使用起来比字符数字容易

27、的多。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这个简单字符重复nRe

28、peat遍来创建一个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);

29、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;返回该串特定位置的字符,

30、nIndex是数组的下标,从0开始,注意nIndex的范围为0到GetLength()-1。TCHAR:是为了统一多语言编码而设计的;ANSI单字符编码           UNICODE双字节字符编码           UTF-8三字节字符编码TCHAR operator(int nIndex) const;用数组下标的方式来返回某个字符,nIndex是数组的下标,从0开始,注意nIndex的

31、范围为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& ope

32、rator = (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 +

33、 (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&a

34、mp; 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 CSt

35、ring& operator += (LPCTSTR lpsz);throw(CMemoryException);比较操作符:只能使用两种CString , LPCTSTRBOOL 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

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

37、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

38、 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;如果长度

39、小于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,则

40、返回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; 不区

41、分大小写字母该函数对两个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对象中

42、提取从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(CMemoryE

43、xception);从一个CString对象中提取从开始位置,长度为nCount的串,返回值是CString对象,注意返回值可能为空。CString Right(int nCount) const;throw(CMemoryException);从一个CString对象中提取从结束位置,长度为nCount的串,返回值是CString对象,注意返回值可能为空。CString SpanIncluding(LPCTSTR lpszCharSet) const;throw(CMemoryException);从原CString对象中第一个字符开始提取字符charactor与指定的LPCTSTR传进行比

44、对,如果该字符charactor在LPCTSTR串中,则继续进行下一个字符是否在LPCTSTR串中的比较,如果不在,则将该字符前的所有字符串返回构造成一个新的CString对象,如果从第一开始就不在LPCTSTR串中,则返回一个空CString对象。CString SpanExcluding(LPCTSTR lpszCharSet) const;throw(CMemoryException);作用与SpanIncluding函数想反,表示从CString对象中一旦找到是LPCTSTR串中字符时就返回一个CString对象。转换函数:void MakeUpper();将CString对象里的字符

45、全部转换为大写字符。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

46、(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对象进行字符或者字符串进行删除操

47、作,从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&

48、quot;lpszFormat详细内容参照:推荐精选里的说明。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相反

49、。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 signddd where d is a single decimal digit, dddd is one or more decimal digits, ddd is two or three decimal

50、 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 dec

51、imal 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 appropria

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1