这里列举的是经常用到的描述符方法的 写在方法里自己实验看看效果: TextBuf.h #ifndef __TEXTBUF_H__ #define __TEXTBUF_H__ // Include Files #include <e32base.h> // Function Prototypes GLDEF_C TInt E32Main(); #endif // __TEXTBUF_H__ TextBuf.cpp #include "TextBuf.h" #include <e32base.h> #include <e32std.h> #include <e32cons.h> // Console #include <Description.h> // Constants _LIT(KTextConsoleTitle, "Console"); _LIT(KTextFailed, " failed, leave code = %d"); _LIT(KTextPressAnyKey, " [press any key]/n"); // Global Variables LOCAL_D CConsoleBase* console; // write all messages to this LOCAL_C void MainL() { CDescription* desc=CDescription::NewL(*console); desc->WaitForKey(); desc->DeletionExample();//改变这里???????? } LOCAL_C void DoStartL() { // Create active scheduler (to run active objects) CActiveScheduler* scheduler = new (ELeave) CActiveScheduler(); CleanupStack::PushL(scheduler); CActiveScheduler::Install(scheduler); MainL(); // Delete active scheduler CleanupStack::PopAndDestroy(scheduler); } // Global Functions GLDEF_C TInt E32Main() { // Create cleanup stack __UHEAP_MARK; CTrapCleanup* cleanup = CTrapCleanup::New(); // Create output console TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize( KConsFullScreen, KConsFullScreen))); if (createError) return createError; // Run application code inside TRAP harness, wait keypress when terminated TRAPD(mainError, DoStartL()); if (mainError) console->Printf(KTextFailed, mainError); console->Printf(KTextPressAnyKey); console->Getch(); delete console; delete cleanup; __UHEAP_MARKEND; return KErrNone; } Description.h #ifndef DESCRIPTION_H #define DESCRIPTION_H // INCLUDES #include <e32std.h> #include <e32base.h> #include <e32cons.h> // CLASS DECLARATION /** * CDescription * */ class CDescription : public CBase { public: // Constructors and destructor /** * Destructor. */ ~CDescription(); /** * Two-phased constructor. */ static CDescription* NewL(CConsoleBase& aconsole); /** * Two-phased constructor. */ static CDescription* NewLC(CConsoleBase& aconsole); void callExampleL(); void doExampleL(); void useTBuf(); void useTBufC(); void useTPtr(); void useTPtrC(); void WaitForKey(); void CompareExample(); void FindExample(); void MatchExample(); void SubstringExample(); void copyExample(); void FillExample(); void AppendExample(); void FormatExample();//{} void caseExample(); void DeletionExample(); private: /** * Constructor for performing 1st stage construction */ CDescription(CConsoleBase& aconsole); /** * EPOC default constructor for performing 2nd stage construction */ void ConstructL(); private: CConsoleBase& iConsole; }; #endif // DESCRIPTION_H Description.cpp #include "Description.h" CDescription::CDescription(CConsoleBase& aConsole):iConsole(aConsole) { // No implementation required } CDescription::~CDescription() { } CDescription* CDescription::NewLC(CConsoleBase& aConsole) { CDescription* self = new (ELeave)CDescription(aConsole); CleanupStack::PushL(self); self->ConstructL(); return self; } CDescription* CDescription::NewL(CConsoleBase& aConsole) { CDescription* self = CDescription::NewLC(aConsole); CleanupStack::Pop(); // self; return self; } void CDescription::ConstructL() { //iconsole=aconsole; } void CDescription::WaitForKey() { _LIT(KMessage,"Press any key to continue/n/n"); iConsole.Printf(KMessage); iConsole.Getch(); } void CDescription::useTBuf() { iConsole.Printf(_L("/n********useTBuf***********/n"));///n是换行 _LIT(KMessage1,"There are cats "); _LIT(KMessage2,"and dogs"); _LIT(KMessage3,"white "); _LIT(KMessage4,"black "); _LIT(KMessage5,"HBufC alloc length is %d"); _LIT(KMessage6,"HBufC ReAlloc length is %d"); _LIT(KMessage7,"The first address of buf is %x"); _LIT(KEnter,"/n"); TBuf<40> buf; buf=KMessage1; iConsole.Printf(buf); iConsole.Printf(KEnter); buf.Append(KMessage2); iConsole.Printf(buf); iConsole.Printf(KEnter); buf.Insert(10,KMessage3); buf.Insert(25,KMessage4); iConsole.Printf(buf); iConsole.Printf(KEnter); HBufC* hbufc=HBufC::New(40);//返回一个指向HBufC的指针 hbufc=buf.Alloc();//申请一块栈内存 利用buf来初始化他 得到的地址赋值给hbufc指针 TPtr ptr1=hbufc->Des(); iConsole.Printf(ptr1);//==iConsole.Printf(*hbufc); iConsole.Printf(KEnter); iConsole.Printf(KMessage5,ptr1.MaxLength()); iConsole.Printf(KEnter); WaitForKey(); hbufc->ReAlloc(50); TPtr ptr2=hbufc->Des(); iConsole.Printf(KMessage6,ptr2.MaxLength());//从定义了最大长度 iConsole.Printf(KEnter); WaitForKey(); iConsole.Printf(KMessage7,buf.Ptr());//得到buf的头地址 iConsole.Printf(KEnter); WaitForKey(); } void CDescription::useTBufC() { iConsole.Printf(_L("/n********useTBufC***********/n"));///n是换行 _LIT(KMessage1,"There are flowers "); _LIT(KMessage2,"and tree"); _LIT(KMessage3,"buautiful "); _LIT(KMessage4,"HBufC alloc length is %d"); _LIT(KMessage5,"HBufC ReAlloc length is %d"); _LIT(KMessage6,"The first address of bufc is %x"); _LIT(KEnter,"/n"); TBufC<40> bufc; bufc=KMessage1; iConsole.Printf(bufc); iConsole.Printf(KEnter); TPtr ptr=bufc.Des();//把不可改变的bufc变为可以改变的描述符赋予ptr 通过ptr来对bufc进行改变的操作 ptr.Append(KMessage2); iConsole.Printf(bufc); iConsole.Printf(KEnter); ptr.Insert(10,KMessage3); iConsole.Printf(bufc); iConsole.Printf(KEnter); HBufC* hbufc=HBufC::New(40); hbufc=bufc.Alloc(); TPtr ptr1=hbufc->Des(); iConsole.Printf(ptr1); iConsole.Printf(KEnter); iConsole.Printf(KMessage4,ptr1.MaxLength()); iConsole.Printf(KEnter); WaitForKey(); hbufc->ReAlloc(50); TPtr ptr2=hbufc->Des(); iConsole.Printf(KMessage5,ptr2.MaxLength()); iConsole.Printf(KEnter); WaitForKey(); iConsole.Printf(KMessage6,bufc.Ptr()); iConsole.Printf(KEnter); WaitForKey(); } void CDescription::useTPtr() { iConsole.Printf(_L("/n******userTPtr*******/n")); _LIT(KMessage1,"There are pandas "); _LIT(KMessage2,"and deers"); _LIT(KMessage3,"small lovely "); _LIT(KMessage4,"HBufC alloc length is %d"); _LIT(KMessage5,"HBufC ReAlloc length is %d"); _LIT(KMessage6,"The first address of ptr is %x"); _LIT(KEnter,"/n"); TBufC<40> bufc(KMessage1); TPtr16 ptr(bufc.Des());//TPtr16 ptr=bufc.Des(); iConsole.Printf(ptr); iConsole.Printf(KEnter); WaitForKey(); ptr.Append(KMessage2); iConsole.Printf(ptr); iConsole.Printf(KEnter); ptr.Insert(10,KMessage3); iConsole.Printf(ptr); iConsole.Printf(KEnter); HBufC* hbufc=HBufC::New(40);// hbufc=ptr.Alloc();// HBufC* hbufc=ptr.Alloc(); TPtr ptr1=hbufc->Des(); iConsole.Printf(ptr1); iConsole.Printf(KEnter); iConsole.Printf(KMessage4,ptr1.MaxLength()); iConsole.Printf(KEnter); WaitForKey(); hbufc->ReAlloc(50); TPtr ptr2=hbufc->Des(); iConsole.Printf(KMessage5,ptr2.MaxLength()); iConsole.Printf(KEnter); WaitForKey(); iConsole.Printf(KMessage6,ptr.Ptr()); iConsole.Printf(KEnter); WaitForKey(); } void CDescription::useTPtrC() { iConsole.Printf(_L("/n******userTPtrC*******/n")); _LIT(KMessage1,"There are book "); _LIT(KMessage2,"HBufC alloc length is %d"); _LIT(KMessage3,"HBufC ReAlloc length is %d"); _LIT(KMessage4,"The first address of ptrc is %x"); _LIT(KEnter,"/n"); TPtrC ptrc(KMessage1); iConsole.Printf(ptrc); iConsole.Printf(KEnter); HBufC* hbufc=HBufC::New(40); hbufc=ptrc.Alloc(); TPtr ptr1=hbufc->Des(); iConsole.Printf(ptr1); iConsole.Printf(KEnter); iConsole.Printf(KMessage2,ptr1.MaxLength()); iConsole.Printf(KEnter); hbufc->ReAlloc(50); TPtr ptr2=hbufc->Des(); iConsole.Printf(KMessage3,ptr2.MaxLength()); iConsole.Printf(KEnter); iConsole.Printf(KMessage4,ptrc.Ptr()); iConsole.Printf(KEnter); } void CDescription::CompareExample() { iConsole.Printf(_L("/n******Test Compare*******/n")); _LIT(KString1,"My String"); _LIT(KString2,"MY STRING"); _LIT(KString3,"Another string"); _LIT(KEnter,"/n"); TBuf<20> str1(KString1); TBuf<20> str2(KString2); TInt res; /*比较描述符是否相同*/ res=str1.Compare(KString1); _LIT(KFormatCompare1,"Compare() str1 and string1= %d/n"); iConsole.Printf(KFormatCompare1,res); res=str1.Compare(str2); _LIT(KFormatCompare2,"Compare() str1 and str2= %d/n"); iConsole.Printf(KFormatCompare2,res); res=str1.CompareF(str2); _LIT(KFormatCompare3,"CompareF() string1 and string2= %d/n"); iConsole.Printf(KFormatCompare3,res); res=str1.CompareF(KString3); _LIT(KFormatCompare4,"CompareF() string1 and string3= %d/n"); iConsole.Printf(KFormatCompare4,res); } void CDescription::FindExample() { iConsole.Printf(_L("/n******Test Find*******/n")); _LIT(KString1,"This is a test string"); _LIT(KString2,"test"); _LIT(KString3,"car"); _LIT(KString4,"TEST"); _LIT(KEnter,"/n"); TBuf<40> buf(KString1); TInt res; /*在KString1中寻找“test” 并且返回其位置*/ res=buf.Find(KString2); _LIT(KFormat1,"Find of string 2 in string1 res=%d/n"); iConsole.Printf(KFormat1,res); /*在KString1中寻找“car” 以为没有所以返回KErrNotFound*/ res=buf.Find(KString3); _LIT(KFormat2,"Find of string 3 in string1 res=%d/n"); iConsole.Printf(KFormat2,res); /*由于是FindF是合并比较模式,所以可以发现“TEST” 并且返回他的位置*/ res=buf.FindF(KString4); _LIT(KFormat3,"Find of string 4 in string1 res=%d/n"); iConsole.Printf(KFormat3,res); } //Match中 "*" 代表人以字符序列 "?"代表任意单个字符 void CDescription::MatchExample() { iConsole.Printf(_L("/n******Test Match*******/n")); _LIT(KString1,"This is test string A"); _LIT(KString2,"This is test string ?"); _LIT(KString3,"*is test string ?"); _LIT(KString4,"*"); _LIT(KString5,"*B"); _LIT(KEnter,"/n"); TBuf<40> buf(KString1); TInt res; res=buf.Match(KString2); _LIT(KFormat1,"Match:string 2 and string 1 res=%d/n"); iConsole.Printf(KFormat1,res);//这个是可以配上的 res=buf.Match(KString3); _LIT(KFormat2,"Match:string 3 and string 1 res=%d/n"); iConsole.Printf(KFormat2,res); res=buf.Match(KString4); _LIT(KFormat3,"Match:string 4 and string 1 res=%d/n"); iConsole.Printf(KFormat3,res); res=buf.Match(KString5); _LIT(KFormat4,"Match:string 5 and string 1 res=%d/n"); iConsole.Printf(KFormat4,res); } void CDescription::SubstringExample() { _LIT(KString1,"This is my string"); TBufC<40> buff(KString1);定义字符串 /*得到左边的4个字符*/ TPtrC SubStr=buff.Left(4);//定义指针型描述符 _LIT(KFormat1,"Left(4):SubStr=/"%S/"/n"); iConsole.Printf(KFormat1,&SubStr); /*得到右边三个字符*/ SubStr.Set(buff.Right(3)); _LIT(KFormat2,"Right(3):SubStr=/"%S/n"); iConsole.Printf(KFormat2,&SubStr); /*从第八个位置开始取六个字符*/ SubStr.Set(buff.Mid(8,6)); _LIT(KFormat3,"Mid(8,6):SubStr=/"%S/n"); iConsole.Printf(KFormat3,&SubStr); } void CDescription::copyExample() { TUint8 binData[6]={0xB0,0xB1,0xB2,0xB3,0xB4,0xB5};//这里的0x是八位 所以才定义TBuf8 /*拷贝标准C语言数组到二进制描述符中*/ TBuf8<sizeof(binData)> binDes; binDes.Copy(binData,sizeof(binData)); _LIT(KFormat1,"binDes(0)=%x binDes[1]=%x/n"); iConsole.Printf(KFormat1,binDes[0],binDes[1]); /*拷贝二进制描述符到另外一个二进制描述符*/ TBuf8<20> buf8; buf8.Copy(binDes); _LIT(KFormat2,"buf8[0]=%x buf[1]=%x/n"); iConsole.Printf(KFormat2,buf8[0],buf8[1]); /*拷贝常量描述符到描述符*/ _LIT(KString1,"My string"); TBuf<20> buf16; buf16.Copy(KString1); _LIT(KFormat3,"buf16=%S/n"); iConsole.Printf(KFormat3,&buf16);//为什么这里要用&了 不明白 /*拷贝8位字符串到描述符*/ TUint8 *C_str =(TUint8 *)"Hello there"; buf8.Copy(C_str); buf16.Copy(buf8); iConsole.Printf(KFormat3,&buf16);//为什么这里要用&了 不明白 /*拷贝并且转为大写*/ TBuf<20> newBuf; newBuf.CopyUC(buf16); _LIT(KFormat4,"CopyUC newBuf=%S/n"); iConsole.Printf(KFormat4,&newBuf);//为什么这里要用&了 不明白 /*拷贝并且转为小写*/ newBuf.CopyLC(buf16); _LIT(KFormat5,"CopyLC newBuf=%S/n"); iConsole.Printf(KFormat5,&newBuf);//为什么这里要用&了 不明白 /*拷贝并且首字母大写*/ newBuf.CopyCP(buf16); _LIT(KFormat6,"CopyCP newBuf=%S/n"); iConsole.Printf(KFormat6,&newBuf);//为什么这里要用&了 不明白 } void CDescription::FillExample() { TBuf<40> buf; buf.Fill('*',10); _LIT(KFormat1,"buf=/"%S/"/n"); iConsole.Printf(KFormat1,&buf); buf.Fill('-'); iConsole.Printf(KFormat1,&buf); } void CDescription::AppendExample() { _LIT(KMyString1,"String:"); _LIT(KMyString2,"num vals are "); _LIT(KMyString3,"Justify"); TInt num=0x0b4a;// TBuf<40> str(KMyString1); /*追加一个字面描述符*/ str.Append(KMyString2); /*以下以各种形式追加数字*/ str.AppendNum(num); str.Append(' '); str.AppendNum(num,EHex); str.Append(' '); str.AppendNumUC(num,EHex);//EHex=16 str.Append(' '); str.AppendNumFixedWidthUC(num,EHex,5);//EHex=16 _LIT(KFormat1,"str=%S/n"); iConsole.Printf(KFormat1,&str); TBuf<40> just; /*对齐字符串Justify a string */ just.AppendJustify(KMyString3,12,ERight,' '); /*在""之间添加任何字符串*/ _LIT(KFormat2,"%S/n"); iConsole.Printf(KFormat2,&just); /*添加重复的字符*/ just.AppendFill('!',5); iConsole.Printf(KFormat2,&just); } void CDescription::FormatExample() { _LIT(KString1,"My string"); TInt value=10,value1=20; _LIT(KMyDesFormat,"Descriptor=%S,value=%d"); TBuf<100> buf; buf.Format(KMyDesFormat,&KString1,value); _LIT(KMyDesFormat1,"--also value1=%d"); buf.AppendFormat(KMyDesFormat1,value1); _LIT(KFormat1,"%S/n"); iConsole.Printf(KFormat1,&buf); } void CDescription::caseExample() { _LIT(KString,"hillary"); TBuf<7> name(KString); name.UpperCase(); _LIT(KFormat1,"name=%S/n"); iConsole.Printf(KFormat1,&name); name.LowerCase(); iConsole.Printf(KFormat1,&name); name.Capitalize(); iConsole.Printf(KFormat1,&name); } void CDescription::DeletionExample() { _LIT(KString1,"Heeeello"); _LIT(KString2," This is a test "); TBuf<20> buf1(KString1); TBuf<40> buf2(KString2); _LIT(KFormat1,"buf1=/"%S/" vuf1.length=%d/n"); iConsole.Printf(KFormat1,&buf1,buf1.Length()); /*删除字符串从 1 开始 长度为 3 */ buf1.Delete(1,3); iConsole.Printf(KFormat1,&buf1,buf1.Length()); _LIT(KFormat2,"buf2=/"%S/" buf2.length=%d/n"); iConsole.Printf(KFormat2,&buf2,buf2.Length()); /*删除开头的空格*/ buf2.TrimLeft(); _LIT(KFormat3,"TrimLeft():buf2=/"%S/" buf2.length=%d/n"); iConsole.Printf(KFormat3,&buf2,buf2.Length()); /*重置描述符*/ buf2.Copy(KString2); /*删除开头的空格*/ buf2.TrimRight(); _LIT(KFormat4,"TrimRight():buf2=/"%S/" buf2.length=%d/n"); iConsole.Printf(KFormat4,&buf2,buf2.Length()); /*删除开头和尾部的空格*/ buf2.Copy(KString2); buf2.Trim(); _LIT(KFormat5,"Trim():buf2=/"%S/" buf2.length=%d/n"); iConsole.Printf(KFormat5,&buf2,buf2.Length()); /*删除所有空格*/ buf2.Copy(KString2); buf2.TrimAll(); _LIT(KFormat6,"TrimALL():buf2=/"%S/" buf2.length=%d/n"); iConsole.Printf(KFormat6,&buf2,buf2.Length()); }