MSDN中关于CString类成员函数的说明Word下载.docx
《MSDN中关于CString类成员函数的说明Word下载.docx》由会员分享,可在线阅读,更多相关《MSDN中关于CString类成员函数的说明Word下载.docx(30页珍藏版)》请在冰点文库上搜索。
![MSDN中关于CString类成员函数的说明Word下载.docx](https://file1.bingdoc.com/fileroot1/2023-4/30/32b26dbb-f009-43b4-978b-0c8fb1c47281/32b26dbb-f009-43b4-978b-0c8fb1c472811.gif)
MakeLower
voidMakeLower();
ConvertsthisCStringobjecttoalowercasestring.
将原对象的所有大写英文字母转换为小写。
(只是将大写的英文字母转换为小写,对于其它的字符不做变化,例如:
小写字符,数字,汉字)
MakeLower.
s.MakeLower();
MakeReverse
voidMakeReverse();
ReversestheorderofthecharactersinthisCStringobject.
将原对象的所有字符颠倒顺序。
MakeReverse.
MakeReverse
s.MakeReverse();
cba"
Replace
intReplace(TCHARchOld,TCHARchNew);
intReplace(LPCTSTRlpszOld,LPCTSTRlpszNew);
ReturnValue
返回值
Thenumberofreplacedinstancesofthecharacter.Zeroifthestringisn'
tchanged.
该函数返回替换的字符数量。
如果原对象没有改变则返回0。
Parameters
参数
chOld
ThecharactertobereplacedbychNew.
将要被chNew所代替的字符。
chNew
ThecharacterreplacingchOld.
用来替换chOld的字符。
lpszOld
ApointertoastringcontainingthecharactertobereplacedbylpszNew.
lpszOld是一个指向字符串的指针,它所包含的字符将被lpszNew所代替。
lpszNew
ApointertoastringcontainingthecharacterreplacinglpszOld.
lpszNew是一个指向字符串的指针,它所包含的字符用来替换lpszOld。
Callthismemberfunctiontoreplaceacharacterwithanother.ThefirstprototypeofthefunctionreplacesinstancesofchOldwithchNewin-placeinthestring.ThesecondprototypeofthefunctionreplacesinstancesofthesubstringlpszOldwithinstancesofthestringlpszNew.
该函数用另外的字符来代替原来的字符。
第一种形态,用chNew就地取代chOld。
第二种形态,用lpszNew来取代原对象的子链lpszOld。
Thestringmaygroworshrinkasaresultofthereplacement;
thatis,lpszNewandlpszOlddonothavetobeequalinlength.Bothversionsperformcase-sensitivematches.
替换后的字符串有可能变长,也有可能缩短,也就是说,lpszNew和lpszOld的长度不必相等。
两个形态都要区别大小写。
//Firstexample,witholdandnewequalinlength.
//第一个例子,长度相等的情况
CStringstrZap("
C--"
);
intn=strZap.Replace('
-'
'
+'
ASSERT(n==2);
ASSERT(strZap=="
C++"
//Secondexample,oldandnewareofdifferentlengths.
//第二个例子,长度不相等的情况
CStringstrBang("
Everybodylikesicehockey"
n=strBang.Replace("
hockey"
"
golf"
ASSERT(n==1);
likes"
plays"
ice"
NULL);
ASSERT(strBang=="
Everybodyplays
golf"
(这里plays和golf之间是两个空格,如果NULL换成"
"
,那么就应该是3个空格)
//notethatyounowhaveanextraspaceinyour
//sentence.Toremovetheextraspace,includeit
//inthestringtobereplaced,i.e.,"
ice"
.
//注意句子中额外的空格。
要消除它,那么被替换的字符串应该是"
。
Remove
intCString:
Remove(TCHARch);
Thecountofcharactersremovedfromthestring.Zeroifthestringisn'
返回原对象中被清除的字符个数。
如果原对象没有改变,则返回0。
ch
Thecharactertoberemovedfromastring.
需要清除的字符。
Callthismemberfunctiontoremoveinstancesofchfromthestring.Comparisonsforthecharacterarecase-sensitive.
该函数用来清除原对象中的字符ch。
大小写不等效。
//removethelower-caseletter'
t'
fromasentence:
//清除句子中的小写t
CStringstr("
Thisisatest."
intn=str.Remove('
ASSERT(str=="
Thisisaes."
Insert
intInsert(intnIndex,TCHARch)
throw(CMemoryException);
intInsert(intnIndex,LPCTSTRpstr)
Thelengthofthechangedstring.
返回改变后的字符串长度。
nIndex
Theindexofthecharacterbeforewhichtheinsertionwilltakeplace.
用来确定插入的位置。
Thecharactertobeinserted.
需要插入的字符。
pstr
Apointertothesubstringtobeinserted.
需要插入的子链的指针。
Callthismemberfunctiontoinsertasinglecharacterorasubstringatthegivenindexwithinthestring.ThenIndexparameteridentifiesthefirstcharacterthatwillbemovedtomakeroomforthecharacterorsubstring.IfnIndexiszero,theinsertionwilloccurbeforetheentirestring.IfnIndexishigherthanthelengthofthestring,thefunctionwillconcatenatethepresentstringandthenewmaterialprovidedbyeitherchorpstr.
该函数用来在原对象中的指定位置插入一个字符或子链。
nIndex参数表示第一个为了给插入的字符或子链让位而被移动的字符。
如果nIndex为0,则在原对象的最前面插入。
如果nIndex大于了原对象的长度,该函数就将ch或者pstr连接到原函数的最后面。
//ThefollowingexampledemonstratestheuseofCString:
Insert.
CStringstr("
HockeyBest"
intn=str.Insert(6,"
is"
ASSERT(n==str.GetLength());
printf("
1:
%s\n"
(LPCTSTR)str);
n=str.Insert(6,'
'
2:
n=str.Insert(555,'
!
'
3:
//thiscodegeneratestheselinesofoutput:
//以上代码产生如下的输出:
HockeyisBest
HockeyisBest
HockeyisBest!
Delete
intDelete(intnIndex,intnCount=1)
Theindexofthefirstcharactertodelete.
表示第一个需要被删除的字符位置。
nCount
Thenumberofcharacterstoberemoved.
需要删除的字符个数。
CallthismemberfunctiontodeleteacharacterorcharactersfromastringstartingwiththecharacteratnIndex.IfnCountislongerthanthestring,theremainderofthestringwillberemoved.
该函数用来删除原对象中从第nIndex+1个字符开始的nCount个字符。
如果nCount比字符串(应该是从第nIndex+1个字符开始的子链)的字符个数大,那么删除的就是从nIndex+1个字符开始的所有字符。
Delete.
str2="
Hockeyisbest!
"
;
Before:
(LPCTSTR)str2);
intn=str2.Delete(6,3);
After:
ASSERT(n==str2.GetLength());
//thiscodegeneratesthislineofoutput:
Hockeyisbest!
Hockeybest!
Format
voidFormat(LPCTSTRlpszFormat,...);
voidFormat(UINTnFormatID,...);
lpszFormat
Aformat-controlstring.
格式控制字符串。
nFormatID
Thestringresourceidentifierthatcontainstheformat-controlstring.
包含格式控制字符串的字符串资源标记。
CallthismemberfunctiontowriteformatteddatatoaCStringinthesamewaythatsprintfformatsdataintoaC-stylecharacterarray.ThisfunctionformatsandstoresaseriesofcharactersandvaluesintheCString.Eachoptionalargument(ifany)isconvertedandoutputaccordingtothecorrespondingformatspecificationinlpszFormatorfromthestringresourceidentifiedbynFormatID.
该函数将数据格式化为CString对象,其用法和使用sprintf函数将数据格式化为C语言风格的字符数组一样。
该函数将一连串的字符和数值格式化并存放到CString对象中。
某变量(如果有)被转换,并且按照lpszFormat或者字符串资源标记nFormatID规定的格式输出。
ThecallwillfailifthestringobjectitselfisofferedasaparametertoFormat.Forexample,thefollowingcode:
如果CString对象本身被当作参数提供给Format,那么函数会调用失败。
例如下面的代码:
CStringstr="
SomeData"
str.Format("
%s%d"
str,123);
//Attention:
strisalsousedintheparameterlist.
//注意:
str也被用作参数
willcauseunpredictableresults.
将导致不可预知的结果。
Whenyoupassacharacterstringasanoptionalargument,youmustcastitexplicitlyasLPCTSTR.Theformathasthesameformandfunctionastheformatargumentfortheprintffunction.(Foradescriptionoftheformatandarguments,seeprintfintheRun-TimeLibraryReference.)Anullcharacterisappendedtotheendofthecharacterswritten.
当把字符串当作参数传递的时候,必须象LPCTSTR一样明确地声明它。
其格式和功能与printf的形参一样(关于格式和参数的说明,参阅Run-TimeLibraryReference中的sprintf函数)。
写入字符的末端没有字符被添加。
Formoreinformation,seesprintfintheRun-TimeLibraryReference.
更多说明参阅Run-TimeLibraryReference(运行库参考手册)中的sprintf函数。
CStringstr;
str.Format(_T("
Floatingpoint:
%.2f\n"
),12345.12345);
_tprintf("
%s"
Left-justifiedinteger:
%.6d\n"
),35);
str.Format(IDS_SCORE,5,3);
Output
输出
IftheapplicationhasastringresourcewiththeidentifierIDS_SCOREthatcontainsthestring"
Penguins:
%d\nFlyers
:
%d\n"
theabovecodefragmentproducesthisoutput:
如果使用包含字符串"
的字符串资源标识符IDS_SCORE,则上面的代码将产生如下输出:
12345.12
000035
5
Flyers
3
FormatV
voidFormatV(LPCTSTRlpszFormat,va_listargList);
argList
Alistofargumentstobepassed.
被传递的一列参数。
Callthismemberfunctiontowriteaformattedstringandavar