企业博客网bokee.net www.bokee.net http://wbymcs51.blog.bokee.net/  CString成员函数使用实例 打印此页

CString成员函数使用实例

http://wbymcs51.blog.bokee.net    2007-2-24

            CString成员函数使用实例

              王佰营   徐丽红

          wbymcs51.blog.bokee.net

   CString类在字符串处理中非常方便,快捷;熟练掌握非常重要.

一. Cstring的构造:
 CString ss1;
 ss1 = "This is my book";
 CString ss2("Good bye!");
 CString ss3 = ss2;
 CString ss4(ss1);
 CString ss5(ss2+"--"+ss3);
 CString ss6(’H’);                 

 //ss6=’H’;
 CString ss7(’Y’,6);     //ss7="YYYYYY";
 CString ss8 = "Very Good!"; //ss7="Very Good!";
 //IDS_ABOUTBOX   101   关于 MCS51(&A)...
 CString ss9((LPCSTR)IDS_ABOUTBOX);  //ss9="MCS51(&A)...";
 LPCTSTR lpsz ="abcd";
 Cstring ss10(lpsz);               

//ss10="abcd";

二. 成员函数;
1. void AnsiToOem( );
 ss1 = "This is my book";
 ss1.AnsiToOem(); // ss1 = "This is my book";(OEM);

2. int Collate(LPCTSTR lpsz)const;
 A>. LPCTSTR  lpsz ="abcd";
     ss1 ="abcd";
  int i=ss1.Collate(lpsz);   // i = 0;
 B>. LPCTSTR  lpsz ="abcc";
     ss1 ="abcd";
  int i=ss1.Collate(lpsz);   // i = 1;
 C>. LPCTSTR  lpsz ="abcf";
     ss1 ="abcd";
  int i=ss1.Collate(lpsz);   // i = -1;
 D>. ss1="abcd";
  int i=ss1.Collate("abcD"); // i = -1;
 E>. ss1="abcd";
  int i=ss1. Collate("ABCD");// i =-1;

3. int CollateNoCase(LPCTSTR lpsz)const;
 A>. LPCTSTR  lpsz ="abcd";
     ss1 ="abcd";
  int i=ss1. CollateNoCase (lpsz);// i = 0;
 B>. LPCTSTR  lpsz ="abcc";
     ss1 ="abcd";
  int i=ss1. CollateNoCase (lpsz);// i = 1;
 C>. LPCTSTR  lpsz ="abcf";
     ss1 ="abcd";
     int i=ss1. CollateNoCase (lpsz);  // i = -1;
 D>. ss1="abcd";
    int i=ss1. CollateNoCase ("abcD");// i = 0;
 E>. ss1="abcd";
   int i=ss1. CollateNoCase ("ABCD");// i =0;

4. int Compare(LPCTSTR lpsz)const;
 A>. LPCTSTR  lpsz ="abcd";
     ss1 ="abcd";
     int i=ss1.Compare (lpsz);   // i = 0;
 B>. LPCTSTR  lpsz ="abcc";
     ss1 ="abcd";
     int i=ss1.Compare (lpsz); // i = 1;
 C>. LPCTSTR  lpsz ="abcf";
     ss1 ="abcd";
     int i=ss1.Compare (lpsz); // i = -1;
 D>. ss1="abcd";
     int i=ss1.Compare ("abcD"); // i = 1;
 E>. ss1="abcd";
     int i=ss1.Compare ("ABCD"); // i = 1;

5. int CompareNoCase(LPCTSTR lpsz)const;
 A>. LPCTSTR  lpsz ="abcd";
     ss1 ="abcd";
     int i=ss1. CompareNoCase (lpsz);// i = 0;
 B>. LPCTSTR  lpsz ="abcc";
     ss1 ="abcd";
     int i=ss1. CompareNoCase (lpsz);// i = 1;
 C>. LPCTSTR  lpsz ="abcf";
     ss1 ="abcd";
     int i=ss1. CompareNoCase (lpsz);// i = -1;
 D>. ss1="abcd";
     int i=ss1. CompareNoCase ("abcD");// i = 0;
 E>. ss1="abcd";
     int i=ss1. CompareNoCase ("ABCD");// i =0;

6. int Delete(int nIndex, int nCount = 1);
 Cstring ss1;
 ss1="0123456789";
 ss1.Delete(3,2);  //ss1 = "01256789";

 ss1="0123456789";
 ss1.Delete(3,10);  //出错!

 ss1="0123456789";
 ss1.Delete(3,0);  //ss1 = "0123456789";

 ss1="0123456789";
 ss1.Delete(15,3);  //ss1 = "0123456789";

 7.void Empty();
 Cstring ss1="0123456789";
 ss1.Empty();   //ss1 = "";

7. int Find(TCHAR ch) const;
  int Find(LPCTSTR lpszSub) const;
  int Find(TCHAR ch, int nStart) const;
  int Find(LPCTSTR lpszSub, int nStart) const;
  int nPos;
  Cstring ss1 = "0123456789";
  nPos = ss1.Find(’5’);        // nPos = 5;
  nPos = ss1.Find(’567’);      // nPos = 7;
  nPos = ss1.Find("567");      // nPos = 7;
  nPos = ss1.Find(’5’,3);      // nPos = 5;
  nPos = ss1.Find(’5’,6);      // nPos = -1;
  nPos = ss1.Find("567",0);    // nPos = 5;
  nPos = ss1.Find("567",9);    // nPos = -1;

8. int FindOneOf(LPCSTR lpszCharSet, int nStart);
  Cstring ss1 = "0123456789";
  nPos = ss1.FindOneOf("A6B");  // nPos = 6
  nPos = ss1.FindOneOf("4A6B"); // nPos = 4

9. void Format(LPCTSTR lpszFormat,....);
  void Format(UINT nFormatID,....);
  Cstring ss1;
  int i = 100;
  ss2.Format("There are %d books on the desk!",i);
  //ss2 = "There are 100 books on the desk!"

10. void FormatV(LPCTSTR lpszFormat,va_list argList);
  LPCTSTR pstr = "1234567890";
  va_list args;
  va_start(args,pstr);
  ss2.FormatV(pstr, args);    // ss2 = "1234567890"
11. void FormatMessage(LPCTSTR lpszFormat,...);
  void FormatMessage(UINT nFormatID,....);
  LPCTSTR pstr="7654321";
  ss2.FormatMessage(pstr);  //ss2 = "7654321";
12.void FreeExtra();

13. TCHAR GetAt(int nIndex) const;
  CString ss1="0123456789";
  char ch = ss1.GetAt(7);// ch = ’7’;

14. PTSTR GetBuffer(int nMinBufLength);
 void ReleaseBuffer(int nNewLength = -1);
 Cstring ss1,ss2;
 ss1="0123456789";
 LPSTR lp = ss1.GetBuffer(10); 
 ss2 = lp;            //ss2 = "0123456789";
 strcpy(lp,"HELO!");  //ss1 = "HELO!";
 ss1.ReleaseBuffer();  //lp所指向的缓冲区无效;

14.LPSTR GetBufferSetLength(int nNewLength);
  Cstring ss1 = "0123456789";
  LPSTR lp = ss1. GetBufferSetLength(3);
  ss1="0123456789";
  lp=ss1.GetBufferSetLength(3);
  ss2=lp;                       //ss2 = "012";
  strcpy(lp,"ABCDE");           //ss1 = "ABCDE";

15. int GetLength() const;
 Cstring ss1 = "0123456789";
 int len = ss1. GetLength();    //len = 10;

16. BOOL IsEmpty() const;
 Cstring ss1 = "0123456789";
 BOOL bEmpty = ss1. IsEmpty(); //bEmpty = faulse;
 ss1 ="";
 bEmpty = ss1. IsEmpty();      //bEmpty = true;

17. Cstring Left(int nCount) cost;
 Cstring ss1 = "0123456789";
 Cstring ss2 = ss1. Left (4); //ss2 = "0123";
 //ss1 = "0123456789";

18. Cstring Right(int nCount) cost; 
 Cstring ss1 = "0123456789";
 Cstring ss2 = ss1. Right (10-4); //ss2 = "456789";
 //ss1 = "0123456789";
19.  BOOL LoadSteing(UINT nID);
 Cstring ss1;
 SD_ID 113 MCS51 &Save as
 ss1.LoadSteing(113);            //ss1 =  "MCS51 &Save as ";
20. LPTSTR LockBuffer();
 Cstring ss1,ss2;
 ss1="0123456789";
 LPSTR lp = ss1.LockBuffer();
 ss2 = lp;                       // ss2 = "0123456789";
 strcpy(lp,"77777");             //ss1 = "77777";

21. void MakeLower();
 Cstring ss1 ="ABCDEF";
 ss1.MakeLower();                //ss1 = "abcdef";

22. void MakeReverse();
 Cstring ss1 ="ABCDEF";
 ss1. MakeReverse ();            //ss1 = "fedcba";

23.  void Mid(int nFirst) const;
 void Mid(int nFirst, nCount) const;
 Cstring ss2,ss1 ="0123456789";
 ss2 = ss1. Mid(3);              // ss2 = "3456789";
 ss2 = ss1. Mid(3,2);            // ss2 = "34";

24.  int Mid(TCHAR ch);
 void Mid(int nFirst, nCount) const;
 Cstring ss2,ss1 ="0123456789";
 ss2 = ss1. Mid(3);              // ss2 = "3456789";
 ss2 = ss1. Mid(3,2);            // ss2 = "34";

25. int Remove(TCHAR ch);
 Cstring ss1;
 ss1="this is a test";   
 int n=ss1.Remove(’t’);          //ss1 = " his is a  es "; 
                                 //n = 3;
26. int Replace(TCHAR chOld, TCHAR chNew);
 int Replace(LPCTSTR lpszOld, LPCTSRR lpszNew);
 Cstring ss1; int n;
 ss1 = "C--";
 n = ss1.Replace(’-’, ’+’);     //ss1 = "C++";
    //n = 2;
 ss1="00,01,02,a0,b0,";
 n=ss1.Replace(",", "H,");      //ss1 = "00H,01H,02H,a0H,b0H,";
                                //n = 5;

27. int ReverseFind(TCHAR ch) const;
 Cstring ss1;
 ss1="this is a test";   
 int n=ss1. ReverseFind (’t’); //n = 13;

28. void SetAt(int nIndex, TCHAR ch) const;
 Cstring  ss1="0123456789";
 ss1.SetAt(4,’H’);             // ss1="0123H56789"

29. int Insert(int nIndex, TCHAR ch);
 int Insert(int nIndex, LPCTSTR psr);
 Cstring  ss1="0123456789";
 ss1. Insert (4,’H’);          // ss1="0123H456789"
 ss1. Insert (4,’Hex’);        // ss1="0123x456789"
 ss1. Insert (4, "Hex ");      // ss1="0123Hex456789"

30. BSTR SetSysString(BSTR *pbstr) const;

31. Cstring SpanExcluding(LPCTSTR lpszCharSet) const;

32. Cstring SpanIncluding(LPCTSTR lpszCharSet) const
 CString  ss1 = "0123456789";
 Cstring  ss2 = ss1.SpanIncluding("0123");//ss2 = "0123";
 ss2 = ss1.SpanIncluding("456"); //ss2 = "";
 ss2 = ss1.SpanIncluding("789"); //ss2 = "";
 ss2 = ss1.SpanIncluding("01234567"); //ss2 = "01234567";

 ss1 = "01234   56789"
 ss2 = ss1.SpanIncluding("01234567"); // ss2 = "01234";

 ss1 = "01234   56789"
 ss2 = ss1.SpanIncluding("012345678"); // ss2 = "01234";

 ss1 = "56789"
 ss2 = ss1.SpanIncluding("012345678"); // ss2 = "5678";

33. void TrimRight();
 void TrimRight(TCHAR chTarget);
 void TrimRight(LPCTSTR lpszTargets);

34. void TrimLeft();
 void TrimLeft(TCHAR chTarget);
 void TrimLeft(LPCTSTR lpszTargets);
35. void UnlockBufer();

关键词:CString C++ VC 成员函数 使用实例