您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 冶金工业 > MFC CString的用法
MFCCString的一些函数2008-12-1111:52CString::MakeUppervoidMakeUpper();Remarks备注ConvertsthisCStringobjecttoanuppercasestring.将原对象的所有小写英文字母转换为大写。(只是将小写的英文字母转换为大写,对于其它的字符不做变化,例如:大写字符,数字,汉字)Example实例ThefollowingexampledemonstratestheuseofCString::MakeUpper.//exampleforCString::MakeUpperCStrings(abc);s.MakeUpper();ASSERT(s==ABC);---------------------------------------------------------------------------------------------------------------------------CString::MakeLowervoidMakeLower();Remarks备注ConvertsthisCStringobjecttoalowercasestring.将原对象的所有大写英文字母转换为小写。(只是将大写的英文字母转换为小写,对于其它的字符不做变化,例如:小写字符,数字,汉字)Example实例ThefollowingexampledemonstratestheuseofCString::MakeLower.//exampleforCString::MakeLowerCStrings(ABC);s.MakeLower();ASSERT(s==abc);---------------------------------------------------------------------------------------------------------------------------CString::MakeReversevoidMakeReverse();Remarks备注ReversestheorderofthecharactersinthisCStringobject.将原对象的所有字符颠倒顺序。Example实例ThefollowingexampledemonstratestheuseofCString::MakeReverse.//exampleforCString::MakeReverseCStrings(abc);s.MakeReverse();ASSERT(s==cba);---------------------------------------------------------------------------------------------------------------------------CString::ReplaceintReplace(TCHARchOld,TCHARchNew);intReplace(LPCTSTRlpszOld,LPCTSTRlpszNew);ReturnValue返回值Thenumberofreplacedinstancesofthecharacter.Zeroifthestringisn'tchanged.该函数返回替换的字符数量。如果原对象没有改变则返回0。Parameters参数chOldThecharactertobereplacedbychNew.将要被chNew所代替的字符。chNewThecharacterreplacingchOld.用来替换chOld的字符。lpszOldApointertoastringcontainingthecharactertobereplacedbylpszNew.lpszOld是一个指向字符串的指针,它所包含的字符将被lpszNew所代替。lpszNewApointertoastringcontainingthecharacterreplacinglpszOld.lpszNew是一个指向字符串的指针,它所包含的字符用来替换lpszOld。Remarks备注Callthismemberfunctiontoreplaceacharacterwithanother.ThefirstprototypeofthefunctionreplacesinstancesofchOldwithchNewin-placeinthestring.ThesecondprototypeofthefunctionreplacesinstancesofthesubstringlpszOldwithinstancesofthestringlpszNew.该函数用另外的字符来代替原来的字符。第一种形态,用chNew就地取代chOld。第二种形态,用lpszNew来取代原对象的子链lpszOld。Thestringmaygroworshrinkasaresultofthereplacement;thatis,lpszNewandlpszOlddonothavetobeequalinlength.Bothversionsperformcase-sensitivematches.替换后的字符串有可能变长,也有可能缩短,也就是说,lpszNew和lpszOld的长度不必相等。两个形态都要区别大小写。Example实例//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);n=strBang.Replace(likes,plays);ASSERT(n==1);n=strBang.Replace(ice,NULL);ASSERT(n==1);ASSERT(strBang==Everybodyplaysgolf);(这里plays和golf之间是两个空格,如果NULL换成,那么就应该是3个空格)//notethatyounowhaveanextraspaceinyour//sentence.Toremovetheextraspace,includeit//inthestringtobereplaced,i.e.,ice.//注意句子中额外的空格。要消除它,那么被替换的字符串应该是ice。---------------------------------------------------------------------------------------------------------------------------CString::RemoveintCString::Remove(TCHARch);ReturnValue返回值Thecountofcharactersremovedfromthestring.Zeroifthestringisn'tchanged.返回原对象中被清除的字符个数。如果原对象没有改变,则返回0。Parameters参数chThecharactertoberemovedfromastring.需要清除的字符。Remarks备注Callthismemberfunctiontoremoveinstancesofchfromthestring.Comparisonsforthecharacterarecase-sensitive.该函数用来清除原对象中的字符ch。大小写不等效。Example实例//removethelower-caseletter't'fromasentence://清除句子中的小写tCStringstr(Thisisatest.);intn=str.Remove('t');ASSERT(n==2);ASSERT(str==Thisisaes.);---------------------------------------------------------------------------------------------------------------------------CString::InsertintInsert(intnIndex,TCHARch)throw(CMemoryException);intInsert(intnIndex,LPCTSTRpstr)throw(CMemoryException);ReturnValue返回值Thelengthofthechangedstring.返回改变后的字符串长度。Parameters参数nIndexTheindexofthecharacterbeforewhichtheinsertionwilltakeplace.用来确定插入的位置。chThecharactertobeinserted.需要插入的字符。pstrApointertothesubstringtobeinserted.需要插入的子链的指针。Remarks备注Callthismemberfunctiontoinsertasinglecharacterorasubstringatthegivenindexwithinthestring.ThenIndexparameteridentifiesthefirstcharacterthatwillbemovedtomakeroomforthecharacterorsubstring.IfnIndexiszero,theinsertionwilloccurbeforetheentirestring.IfnIndexishigherthanthelengthofthestring,thefunctionwillconcatenatethepresentstringandthenewmaterialprovidedbyeitherchorpstr.该函数用来在原对象中的指定位置插入一个字符或子链。nIndex参数表示第一个为了给插入的字符或子链让位而被移动的字符。如果nIndex为0,则在原对象的最前面插入。如果nIndex大于了原对象的长度,该函数就将ch或者pstr连接到原函数的最后面。Example实例//ThefollowingexampledemonstratestheuseofCString::Insert.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,'!');ASSERT(n==str.GetLength());printf(3:%s\n,(LPCTSTR)str);//thiscodegeneratestheselinesofoutput://以上代码产生如下的输出:1:HockeyisBest2:HockeyisBest3:HockeyisBest!--------------------------------------------------------------------------------------------------------------------
本文标题:MFC CString的用法
链接地址:https://www.777doc.com/doc-4439715 .html