MFC CString的用法.docx

上传人:b****8 文档编号:9010124 上传时间:2023-05-16 格式:DOCX 页数:31 大小:25.71KB
下载 相关 举报
MFC CString的用法.docx_第1页
第1页 / 共31页
MFC CString的用法.docx_第2页
第2页 / 共31页
MFC CString的用法.docx_第3页
第3页 / 共31页
MFC CString的用法.docx_第4页
第4页 / 共31页
MFC CString的用法.docx_第5页
第5页 / 共31页
MFC CString的用法.docx_第6页
第6页 / 共31页
MFC CString的用法.docx_第7页
第7页 / 共31页
MFC CString的用法.docx_第8页
第8页 / 共31页
MFC CString的用法.docx_第9页
第9页 / 共31页
MFC CString的用法.docx_第10页
第10页 / 共31页
MFC CString的用法.docx_第11页
第11页 / 共31页
MFC CString的用法.docx_第12页
第12页 / 共31页
MFC CString的用法.docx_第13页
第13页 / 共31页
MFC CString的用法.docx_第14页
第14页 / 共31页
MFC CString的用法.docx_第15页
第15页 / 共31页
MFC CString的用法.docx_第16页
第16页 / 共31页
MFC CString的用法.docx_第17页
第17页 / 共31页
MFC CString的用法.docx_第18页
第18页 / 共31页
MFC CString的用法.docx_第19页
第19页 / 共31页
MFC CString的用法.docx_第20页
第20页 / 共31页
亲,该文档总共31页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

MFC CString的用法.docx

《MFC CString的用法.docx》由会员分享,可在线阅读,更多相关《MFC CString的用法.docx(31页珍藏版)》请在冰点文库上搜索。

MFC CString的用法.docx

MFCCString的用法

MFCCString的一些函数2008-12-1111:

52CString:

:

MakeUpper

voidMakeUpper();

Remarks

备注ConvertsthisCStringobjecttoanuppercasestring.

将原对象的所有小写英文字母转换为大写。

(只是将小写的英文字母转换为大写,对于其它的字符不做变化,例如:

大写字符,数字,汉字)

Example实例

ThefollowingexampledemonstratestheuseofCString:

:

MakeUpper.

//exampleforCString:

:

MakeUpper

CStrings("abc");

s.MakeUpper();

ASSERT(s=="ABC");

---------------------------------------------------------------------------------------------------------------------------

CString:

:

MakeLower

voidMakeLower();

Remarks备注

ConvertsthisCStringobjecttoalowercasestring.

将原对象的所有大写英文字母转换为小写。

(只是将大写的英文字母转换为小写,对于其它的字符不做变化,例如:

小写字符,数字,汉字)

Example实例

ThefollowingexampledemonstratestheuseofCString:

:

MakeLower.

//exampleforCString:

:

MakeLower

CStrings("ABC");

s.MakeLower();

ASSERT(s=="abc");

---------------------------------------------------------------------------------------------------------------------------

CString:

:

MakeReverse

voidMakeReverse();

Remarks备注

ReversestheorderofthecharactersinthisCStringobject.

将原对象的所有字符颠倒顺序。

Example实例

ThefollowingexampledemonstratestheuseofCString:

:

MakeReverse.

//exampleforCString:

:

MakeReverse

CStrings("abc");

s.MakeReverse();

ASSERT(s=="cba");

---------------------------------------------------------------------------------------------------------------------------

CString:

:

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。

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:

:

Remove

intCString:

:

Remove(TCHARch);

ReturnValue返回值

Thecountofcharactersremovedfromthestring.Zeroifthestringisn'tchanged.返回原对象中被清除的字符个数。

如果原对象没有改变,则返回0。

Parameters参数

ch

Thecharactertoberemovedfromastring.需要清除的字符。

Remarks备注

Callthismemberfunctiontoremoveinstancesofchfromthestring.Comparisonsforthecharacterarecase-sensitive.该函数用来清除原对象中的字符ch。

大小写不等效。

Example实例

//removethelower-caseletter't'fromasentence:

//清除句子中的小写t

CStringstr("Thisisatest.");

intn=str.Remove('t');

ASSERT(n==2);

ASSERT(str=="Thisisaes.");

---------------------------------------------------------------------------------------------------------------------------

CString:

:

Insert

intInsert(intnIndex,TCHARch)

throw(CMemoryException);

intInsert(intnIndex,LPCTSTRpstr)

throw(CMemoryException);

ReturnValue返回值

Thelengthofthechangedstring.返回改变后的字符串长度。

Parameters参数

nIndex

Theindexofthecharacterbeforewhichtheinsertionwilltakeplace.用来确定插入的位置。

ch

Thecharactertobeinserted.需要插入的字符。

pstr

Apointertothesubstringtobeinserted.需要插入的子链的指针。

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:

HockeyisBest

2:

HockeyisBest

3:

HockeyisBest!

---------------------------------------------------------------------------------------------------------------------------

CString:

:

Delete

intDelete(intnIndex,intnCount=1)

throw(CMemoryException);

ReturnValue

返回值

Thelengthofthechangedstring.返回改变后的字符串长度。

Parameters参数

nIndex

Theindexofthefirstcharactertodelete.表示第一个需要被删除的字符位置。

nCount

Thenumberofcharacterstoberemoved.需要删除的字符个数。

Remarks备注

CallthismemberfunctiontodeleteacharacterorcharactersfromastringstartingwiththecharacteratnIndex.IfnCountislongerthanthestring,theremainderofthestringwillberemoved.

该函数用来删除原对象中从第nIndex+1个字符开始的nCount个字符。

如果nCount比字符串(应该是从第nIndex+1个字符开始的子链)的字符个数大,那么删除的就是从nIndex+1个字符开始的所有字符。

Example实例

//ThefollowingexampledemonstratestheuseofCString:

:

Delete.

str2="Hockeyisbest!

";

printf("Before:

%s\n",(LPCTSTR)str2);

intn=str2.Delete(6,3);

printf("After:

%s\n",(LPCTSTR)str2);

ASSERT(n==str2.GetLength());

//thiscodegeneratesthislineofoutput:

Before:

Hockeyisbest!

After:

Hockeybest!

---------------------------------------------------------------------------------------------------------------------------

CString:

:

Format

voidFormat(LPCTSTRlpszFormat,...);

voidFormat(UINTnFormatID,...);

Parameters参数

lpszFormat

Aformat-controlstring.格式控制字符串。

nFormatID

Thestringresourceidentifierthatcontainstheformat-controlstring.包含格式控制字符串的字符串资源标记。

Remarks备注

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函数。

Example实例

CStringstr;

str.Format(_T("Floatingpoint:

%.2f\n"),12345.12345);

_tprintf("%s",(LPCTSTR)str);

str.Format(_T("Left-justifiedinteger:

%.6d\n"),35);

_tprintf("%s",(LPCTSTR)str);

str.Format(IDS_SCORE,5,3);

_tprintf("%s",(LPCTSTR)str);

Output输出

IftheapplicationhasastringresourcewiththeidentifierIDS_SCOREthatcontainsthestring"Penguins:

%d\nFlyers:

%d\n",theabovecodefragmentproducesthisoutput:

如果使用包含字符串"Penguins:

%d\nFlyers:

%d\n"的字符串资源标识符IDS_SCORE,则上面的代码将产生如下输出:

Floatingpoint:

12345.12

Left-justifiedinteger:

000035

Penguins:

5

Flyers:

3

CString:

:

FormatV

voidFormatV(LPCTSTRlpszFormat,va_listargList);

Parameters参数

lpszFormat

Aformat-controlstring.格式控制字符串。

argList

Alistofargumentstobepassed.

被传递的一列参数。

Remarks备注

CallthismemberfunctiontowriteaformattedstringandavariablelistofargumentstoaCStringobjectinthesamewaythatvsprintfformatsdataintoaC-stylecharacterarray.ThisfunctionformatsandstoresaseriesofcharactersandvaluesintheCString.ThestringandargumentsareconvertedandoutputaccordingtothecorrespondingformatspecificationinlpszFormat.

该函数返回一个具有一定格式和一个参数表的CString对象(?

),就象vsprintf函数将数据格式化为C风格的字符数组一样。

该函数格式化并储存一列字符和数值在CString中。

字符串和参数按指定的格式格式化并输出。

Thecallwillfail

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

当前位置:首页 > 高中教育 > 其它课程

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2