CString类所有成员函数详解.docx
- 文档编号:16346283
- 上传时间:2023-07-12
- 格式:DOCX
- 页数:15
- 大小:22.60KB
CString类所有成员函数详解.docx
《CString类所有成员函数详解.docx》由会员分享,可在线阅读,更多相关《CString类所有成员函数详解.docx(15页珍藏版)》请在冰点文库上搜索。
CString类所有成员函数详解
CString类所有成员函数详解
2009-03-2710:
53
CString类所有成员函数详解
VC里CString是我们最常用的类之一,我们觉得对它很熟悉了,可是你知道它的所有用法吗?
还是系统的学习一下吧,认真看完本文就OK了。
下面开始:
CString:
:
Compare
intCompare(LPCTSTRlpsz)const;
返回值字符串一样返回0
小于lpsz返回-1
大于lpsz返回1
区分大小字符
CStrings1("abc");
CStrings2("abd");
ASSERT(s1.Compare(s2)==-1);
ASSERT(s1.Compare("abe")==-1);
CString:
:
CompareNoCase
intCompareNoCase(LPCTSTRlpsz)const;
返回值字符串一样返回0
小于lpsz返回-1
大于lpsz返回1
不区分大小字符
CString:
:
Collate
intCollate(LPCTSTRlpsz)const;
同CString:
:
Compare
CString:
:
CollateNoCase
intCollateNocase(LPCTSTRlpsz)const;
同CString:
:
CompareNoCase
CString:
:
CString
CString();
CString(constCString&stringSrc);
CString(TCHARch,intnRepeat=1);
CString(LPCTSTRlpch,intnLength);
CString(constunsignedchar*psz);
CString(LPCWSTRlpsz);
CString(LPCSTRlpsz);
例子最容易说明问题
CStrings1;
CStrings2("cat");
CStrings3=s2;
CStrings4(s2+""+s3);
CStrings5('x'); //s5="x"
CStrings6('x',6); //s6="xxxxxx"
CStrings7((LPCSTR)ID_FILE_NEW); //s7="Createanewdocument"
CStringcity="Philadelphia";
CString:
:
Delete
intDelete(intnIndex,intnCount=1);
返回值是被删除前的字符串的长度
nIndex是第一个被删除的字符,nCount是一次删除几个字符。
根据我实验得出的结果:
当nCount>要删除字符串的最大长度(GetCount()-nIndex)时会出错,当nCount过大,没有足够的字符删除时,此函数不执行。
例子
CStringstr1,str2,str3;
chara;
str1="nihao";
str2="nIhao";
intx;
//inti=(str1==str2);
str1.Delete(2,3);
如果nCount(3)>GetCount()–nIndex(5-2)就会执行错误
CString:
:
Empty
VoidEmpty();
没有返回值清空操作;
例子
CStrings("abc");
s.Empty();
ASSERT(s.GetLength()==0);
CString:
:
Find
intFind(TCHARch)const;
intFind(LPCTSTRlpszSub)const;
intFind(TCHARch,intnStart)const;
intFind(LPCTSTRlpszSub,intnStart)const;
返回值不匹配的话返回-1;索引以0开始
nStar代表以索引值nStart的字符开始搜索,
即为包含以索引nStart字符后的字符串
例子
CStrings("abcdef");
ASSERT(s.Find('c')==2);
ASSERT(s.Find("de")==3);
Cstringstr(“Thestarsarealigned”);
Ingn=str.Find('e',5);
ASSERT(n==12)
CString:
:
FindOneOf
intFindOneOf(LPCTSTRlpszCharSet)const;
返回值不匹配的话返回-1;索引以0开始
注意:
:
返回此字符串中第一个在lpszCharSet中也包括字符并且从零开始的索引值
例子
CStrings("abcdef");
ASSERT(s.FindOneOf("xd")==3);//'d'isfirstmatch.
CString:
:
Format
voidFormat(LPCTSTRlpszFormat,...);
voidFormat(UINTnFormatID,...);
lpszFormat一个格式控制字符串
nFormatID字符串标识符
例子
CStringstr;
Str.Format(“%d”,13);
此时Str为13
CString:
:
GetAt
TCHARGetAt(intnIndex)const;
返回标号为nIndex的字符,你可以把字符串理解为一个数组,GetAt类似于[].注意nIndex的范围,如果不合适会有调试错误。
CString:
:
GetBuffer
LPTSTRGetBuffer(intnMinBufLength);
返回值
一个指向对象的(以空字符结尾的)字符缓冲区的LPTSTR指针。
参数
nMinBufLength
字符缓冲区的以字符数表示的最小容量。
这个值不包括一个结尾的空字符的空间。
说明
此成员函数返回一个指向CString对象的内部字符缓冲区的指针。
返回的LPTSTR不是const,因此可以允许直接修改CString的内容。
如果你使用由GetBuffer返回的指针来改变字符串的内容,你必须在使用其它的CString成员函数之前调用ReleaseBuffer函数。
在调用ReleaseBuffer之后,由GetBuffer返回的地址也许就无效了,因为其它的CString操作可能会导致CString缓冲区被重新分配。
如果你没有改变此CString的长度,则缓冲区不会被重新分配。
当此CString对象被销毁时,其缓冲区内存将被自动释放。
注意,如果你自己知道字符串的长度,则你不应该添加结尾的空字符。
但是,当你用ReleaseBuffer来释放该缓冲区时,你必须指定最后的字符串长度。
如果你添加了结尾的空字符,你应该给ReleaseBuffer的长度参数传递-1,ReleaseBuffer将对该缓冲区执行strlen来确定它的长度。
下面的例子说明了如何用CString:
:
GetBuffer。
//CString:
:
GetBuffer例子
CStrings("abcd");
#ifdef_DEBUG
afxDump<<"CStrings"<
#endif
LPTSTRp=s.GetBuffer(10);
strcpy(p,"Hello");//直接访问CString对象。
s.ReleaseBuffer();
#ifdef_DEBUG
afxDump<<"CStrings"<
#endif
CString:
:
GetLength
intGetLength()const;
返回值
返回字符串中的字节计数。
说明
此成员函数用来获取这个CString对象中的字节计数。
这个计数不包括结尾的空字符。
对于多字节字符集(MBCS),GetLength按每一个8位字符计数;即,在一个多字节字符中的开始和结尾字节被算作两个字节。
示例
下面的例子说明了如何使用CString:
:
GetLength。
//CString:
:
GetLength示例
CStrings("abcdef");
ASSERT(s.GetLength()==6);
CString:
:
Insert
intInsert(intnIndex,TCHARch);
intInsert(intnIndex,LPCTSTRpstr);
返回修改后的长度,nIndex是字符(或字符串)插入后的索引号例子
CStringstr(“HockeyBest”);
intn=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.HockeyisBest
2.HockeyisBest
3.HockeyisBest!
CString:
:
IsEmpty
BOOLIsEmpty()const;
返回值
如果CString对象的长度为0,则返回非零值;否则返回0。
说明
此成员函数用来测试一个CString对象是否是空的。
示例
下面的例子说明了如何使用CString:
:
IsEmpty。
//CString:
:
IsEmpty示例
CStrings;
ASSERT(s.IsEmpty());
请参阅CString:
:
GetLength
CString:
:
LeftCString:
:
Right
CStringLeft(intnCount)const;
throw(CMemoryException);
返回的字符串是前nCount个字符。
例子
CStrings(_T("abcdef"));
ASSERT(s.Left
(2)==_T("ab"));
CString:
:
LoadString
BOOLLoadString(UINTnID);
throw(CMemoryException);
返回值
如果加载资源成功则返回非零值;否则返回0。
nID一个Windows字符串资源ID。
说明此成员函数用来读取一个由nID标识的Windows字符串资源,并放入一个已有CString对象中。
示例
下面的例子说明了如何使用CString:
:
LoadString。
//CString:
:
LoadString示例
#defineIDS_FILENOTFOUND1
CStrings;
if(!
s.LoadString(IDS_FILENOTFOUND))
CString:
:
MakeLower
voidMakeLower();
改变字符的小写
CString:
:
MakeReverse
voidMakeReverse();
字符倒置
CString:
:
MakeUpper
voidMakeUpper();
改变字符的大写
CString:
:
Mid
CStringMid(intnFirst)const;
CStringMid(intnFirst,intnCount)const;
nCount代表要提取的字符数,nFirst代表要提取的开始索引位置
例子
CStrings(_T("abcdef"));
ASSERT(s.Mid(2,3)==_T("cde"));
CString:
:
ReleaseBuffer
voidReleaseBuffer(intnNewLength=-1);
参数
nNewLength
此字符串的以字符数表示的新长度,不计算结尾的空字符。
如果这个字
符串是以空字符结尾的,则参数的缺省值-1将把CString的大小设置为
字符串的当前长度。
说明
使用ReleaseBuffer来结束对由GetBuffer分配的缓冲区的使用。
如果你知道缓
冲区中的字符串是以空字符结尾的,则可以省略nNewLength参数。
如果字符
串不是以空字符结尾的,则可以使用nNewLength指定字符串的长度。
在调用
ReleaseBuffer或其它CString操作之后,由GetBuffer返回的地址是无效的。
示例
下面的例子说明了如何使用CString:
:
ReleaseBuffer。
//CString:
:
ReleaseBuffer示例
CStrings;
s="abc";
LPTSTRp=s.GetBuffer(1024);
strcpy(p,"abc");//直接使用该缓冲区
ASSERT(s.GetLength()==3);//字符串长度=3
s.ReleaseBuffer();//释放多余的内存,现在p无效。
ASSERT(s.GetLength()==3);//长度仍然是3
CString:
:
Remove
intCString:
:
Remove(TCHARch);
返回值
返回从字符串中移走的字符数。
如果字符串没有改变则返回零。
参数
ch
要从一个字符串中移走的字符。
说明
此成员函数用来将ch实例从字符串中移走。
与这个字符的比较是区分大小写
的。
示例
//从一个句子中移走小写字母'c':
CStringstr(“Thisisatest.”);
intn=str.Remove('t');
ASSERT(n==2);
ASSERT(str==“Thisisaes.”);
CString:
:
Replace
intReplace(TCHARchOld,TCHARchNew);
intReplace(LPCTSTRlpszOld,LPCTSTRlpszNew);
返回值
返回被替换的字符数。
如果这个字符串没有改变则返回零。
参数
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(constCString&stringSrc);
Throw(CMemoryException);
该构造函数表示将一个已经存在的CString对象拷贝到声明的CString对象中。
CString(TCHARch,intnRepeat=1);
throw(CMemoryException);
该构造函数表示将ch这个简单字符重复nRepeat遍来创建一个CString对象。
CString(LPCTSTRlpch,intnLength);
throw(CMemoryException);
构造一个长度为nLength的CString对象,该对象初始化值为一个字符串数组常量指针。
CString(constunsignedchar*psz);
Throw(CMemoryException);
根据无符号字符指针创建CString对象。
CString(LPCWSTRlpsz);
throw(CMemoryException);
根据一个unicode字符串来创建一个CString对象。
CString(LPCSTRlpsz);
throw(CMemoryException);
根据一个ansi字符串来创建一个unicodeCString对象。
作为数组的串:
intGetLength()const;
返回该串的字节数,该函数返回值不包括空结束符”\0”,如果该CString对象被声明成unicode字符集,则会按照一个字符两个字节来计算CString对象的长度。
BOOLIsEmpty()const;
如果CString对象的长度为0则返回为非0,否则返回0值。
voidEmpty();
将CString对象设置为空串并且相应的释放掉内存。
TCHARGetAt(intnIndex)const;
返回该串特定位置的字符,nIndex是数组的下标,从0开始,注意nIndex的范围为0到GetLength()-1。
TCHAR:
是为了统一多语言编码而设计的;
ANSI-单字符编码
UNICODE-双字节字符编码
UTF-8-三字节字符编码
TCHARoperator[](intnIndex)const;
用数组下标的方式来返回某个字符,nIndex是数组的下标,从0开始,注意nIndex的范围为0到GetLength()-1。
注意你不能通过这种方式来改变该位置的字符。
voidSetAt(intnIndex,TCHARch);
将CSring对象中nIndex位置的字符改为ch,注意nIndex的范围为0到GetLength()-1,注意改函数只能修改某个位置上的字符,不能增加字符。
operatorLPCTSTR()const;
返回串数据的字符指针
串合并操作:
constCString&operator=(constCString&stringSrc);
throw(CMemoryException);
constCString&operator=(TCHARch);
throw(CMemoryException);
constCString&operator=(constunsignedchar*psz);
throw(CMemoryException);
constCString&operator=(LPCWSTRlpsz);
throw(CMemoryException);
constCString&operator=(LPCSTRlpsz);
throw(CMemoryException);
赋值操作,不考虑对象的长度。
friendCStringoperator+(constCString&string1,constCString&string2);
throw(CMmeoryException);
friendCStringoperator+(constCString&string,TCHARch);
throw(CMmeoryException);
friendCStringoperator+(TCHARch,constCString&string);
throw(CMmeoryException);
friendCStringoperator+(constCString&string,LPCTSTRlpsz);
throw(CMmeoryException);
friendCStringoperator+(LPCTSTRlpsz,constCString&string);
throw(CMmeoryException);
constCString&operator+=(constCString&string);
throw(CMemoryException);
constCString&operator+=(TCHARch);
throw(CMemoryException);
constCString&operator+=(LPCTSTRlpsz);
throw(CMemoryException);
比较操作符:
只能使用两种CString,LPCTSTR
BOOLoperator==(constCString&s1,constCString&s2);
BOOLoperator==(constCString&s1,LPCTSTRs2);
BOOLoperator==(LPCTSTRs1,constCString&s2);
BOOLoperator!
=(constCString&s1,constCString&s2);
BOOLoperator!
=(constCString&s1,LPCTSTRs2);
BOOLoperator!
=(LPCTSTRs1,constCString&s2);
BOOLoperator<=(constCString&s1,constCString&s2);
BOOLoperator<=(constCString&
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CString 所有 成员 函数 详解