CString成員函式詳解
阿新 • • 發佈:2018-11-23
CString的建構函式
1、CString( );
CString csStr;
2、CString( const CString& stringSrc );
CString csStr("ABCDEF中文123456");
CString csStr2(csStr);
3、CString( TCHAR ch, int nRepeat = 1 );
CString csStr('a',5);
//csStr="aaaaa"
4、CString( LPCTSTR lpch, int nLength );
CString csStr("abcdef",3); //csStr="abc"
5、CString( LPCWSTR lpsz );
wchar_t s[]=L"abcdef";
CString csStr(s);
//csStr=L"abcdef"
6、CString( const unsigned char* psz );
const unsigned char s[]="abcdef";
const unsigned char* sp=s;
CString csStr(sp);
//csStr="abcdef"
7、CString( LPCSTR lpsz );
CString csStr("abcdef");
//csStr="abcdef"
int GetLength( ) const;
//返回字串的長度,不包含結尾的空字元。
csStr="ABCDEF中文123456";
printf("%d",csStr.GetLength()); //16
void MakeReverse( );
//顛倒字串的順序
csStr="ABCDEF中文123456";
csStr.MakeReverse();
cout<<csStr; //654321文中FEDCBA
void MakeUpper( );
//將小寫字母轉換為大寫字母 csStr="abcdef中文123456"; csStr.MakeUpper(); cout<<csStr; //ABCDEF中文123456
void MakeLower( );
//將大寫字母轉換為小寫字母
csStr="ABCDEF中文123456";
csStr.MakeLower();
cout<<csStr; //abcdef中文123456
int Compare( LPCTSTR lpsz ) const;
//區分大小寫比較兩個字串,相等時返回0,大於時返回1,小於時返回-1
csStr="abcdef中文123456";
csStr2="ABCDEF中文123456";
cout<<csStr.CompareNoCase(csStr2); //0
int CompareNoCase( LPCTSTR lpsz ) const;
//不區分大小寫比較兩個字串,相等時返回0,大於時返回1,小於時返回-1
csStr="abcdef中文123456";
csStr2="ABCDEF中文123456";
cout<<csStr.CompareNoCase(csStr2); //-1
int Delete( int nIndex, int nCount = 1 )
//刪除字元,刪除從下標nIndex開始的nCount個字元
csStr="ABCDEF";
csStr.Delete(2,3);
cout<<csStr; // ABF
//當nIndex過大,超出對像所在記憶體區域時,函式沒有任何操作。
//當nIndex為負數時,從第一個字元開始刪除。
//當nCount過大,導致刪除字元超出對像所在記憶體區域時,會發生無法預料的結果。
//當nCount為負數時,函式沒有任何操作。
int Insert( int nIndex, TCHAR ch )
int Insert( int nIndex, LPCTSTR pstr )
//在下標為nIndex的位置,插入字元或字串。返回插入後物件的長度
csStr="abc";
csStr.Insert(2,'x');
cout<<csStr; //abxc
csStr="abc";
csStr.Insert(2,"xyz");
cout<<csStr; //abxyzc
//當nIndex為負數時,插入在物件開頭
//當nIndex超出物件末尾時,插入在物件末尾
int Remove( TCHAR ch );
//移除物件內的指定字元。返回移除的數目
csStr="aabbaacc";
csStr.Remove('a');
cout<<csStr; //bbcc
int Replace( TCHAR chOld, TCHAR chNew );
int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );
//替換字串
csStr="abcdef";
csStr.Replace('a','x');
cout<<csStr; //xbcdef
csStr="abcdef";
csStr.Replace("abc","xyz");
cout<<csStr; //xyzdef
void TrimLeft( );
void TrimLeft( TCHAR chTarget );
void TrimLeft( LPCTSTR lpszTargets );
//從左刪除字元,被刪的字元與chTarget或lpszTargets匹配,一直刪到第一個不匹配的字元為止
csStr="aaabaacdef";
csStr.TrimLeft('a');
cout<<csStr; //baacdef
csStr="aaabaacdef";
csStr.TrimLeft("ab");
cout<<csStr; //cdef
//無引數時刪除空格
void TrimRight( );
void TrimRight( TCHAR chTarget );
void TrimRight( LPCTSTR lpszTargets );
//從右刪除字元,被刪的字元與chTarget或lpszTargets匹配,一直刪到第一個不匹配的字元為止
csStr="abcdeaafaaa";
csStr.TrimRight('a');
cout<<csStr; //abcdeaaf
csStr="abcdeaafaaa";
csStr.TrimRight("fa");
cout<<csStr; //abcde
//無引數時刪除空格
void Empty( );
//清空
csStr="abcdef";
csStr.Empty();
printf("%d",csStr.GetLength()); //0
BOOL IsEmpty( ) const;
//測試物件是否為空,為空時返回零,不為空時返回非零
csStr="abc";
cout<<csStr.IsEmpty(); //0;
csStr.Empty();
cout<<csStr.IsEmpty(); //1;
int Find( TCHAR ch ) const;
int Find( LPCTSTR lpszSub ) const;
int Find( TCHAR ch, int nStart ) const;
int Find( LPCTSTR pstr, int nStart ) const;
//查詢字串,nStart為開始查詢的位置。未找到匹配時返回-1,否則返回字串的開始位置
csStr="abcdef";
cout<<csStr.Find('b'); //1
cout<<csStr.Find("de"); //3
cout<<csStr.Find('b',3); //-1
cout<<csStr.Find('b',0); //1
cout<<csStr.Find("de",4); //-1
cout<<csStr.Find("de",0); //3
//當nStart超出物件末尾時,返回-1。
//當nStart為負數時,返回-1。
int FindOneOf( LPCTSTR lpszCharSet ) const;
//查詢lpszCharSet中任意一個字元在CString物件中的匹配位置。未找到時返回-1,否則返回字串的開始位置
csStr="abcdef";
cout<<csStr.FindOneOf("cxy"); //2
CString SpanExcluding( LPCTSTR lpszCharSet ) const;
//返回物件中與lpszCharSet中任意匹配的第一個字元之前的子串
csStr="abcdef";
cout<<csStr.SpanExcluding("cf"); //ab
CString SpanIncluding( LPCTSTR lpszCharSet ) const;
//從物件中查詢與lpszCharSe中任意字元不匹配的字元,並返回第一個不匹配字元之前的字串
csStr="abcdef";
cout<<csStr.SpanIncluding("fdcba"); //abcd
int ReverseFind( TCHAR ch ) const;
//從後向前查詢第一個匹配,找到時返回下標。沒找到時返回-1
csStr="abba";
cout<<csStr.ReverseFind('a'); //3
oid Format( LPCTSTR lpszFormat, ... );
void Format( UINT nFormatID, ... );
//格式化物件,與C語言的sprintf函式用法相同
csStr.Format("%d",13);
cout<<csStr; //13
TCHAR GetAt( int nIndex ) const;
//返回下標為nIndex的字元,與字串的[]用法相同
csStr="abcdef";
cout<<csStr.GetAt(2); //c
//當nIndex為負數或超出物件末尾時,會發生無法預料的結果。
void SetAt( int nIndex, TCHAR ch );
//給下標為nIndex的字元重新賦值
csStr="abcdef";
csStr.SetAt(2,'x');
cout<<csStr; //abxdef
//當nIndex為負數或超出物件末尾時,會發生無法預料的結果。
CString Left( int nCount ) const;
//從左取字串
csStr="abcdef";
cout<<csStr.Left(3); //abc
//當nCount等於0時,返回空。
//當nCount為負數時,返回空。
//當nCount大於物件長度時,返回值與物件相同。
CString Right( int nCount ) const;
//從右取字串
csStr="abcdef";
cout<<csStr.Right(3); //def
//當nCount等於0時,返回空。
//當nCount為負數時,返回空。
//當nCount大於物件長度時,返回值與物件相同。
CString Mid( int nFirst ) const;
CString Mid( int nFirst, int nCount ) const;
//從中間開始取字串
csStr="abcdef";
cout<<csStr.Mid(2); //cdef
csStr="abcdef";
cout<<csStr.Mid(2,3); //cde
//當nFirst為0和為負數時,從第一個字元開始取。
//當nFirst等於物件末尾時,返回空字串。
//當nFirst超出物件末尾時,會發生無法預料的結果。
//當nCount超出物件末尾時,返回從nFirst開始一直到物件末尾的字串
//當nCount為0和為負數時,返回空字串。
LPTSTR GetBuffer( int nMinBufLength );
//申請新的空間,並返回指標
csStr="abcde";
LPTSTR pStr=csStr.GetBuffer(10);
strcpy(pStr,"12345");
csStr.ReleaseBuffer();
pStr=NULL;
cout<<csStr //12345
//使用完GetBuffer後,必須使用ReleaseBuffer以更新物件內部資料,否則會發生無法預料的結果。
void ReleaseBuffer( int nNewLength = -1 );
//使用GetBuffer後,必須使用ReleaseBuffer以更新物件內部資料
csStr="abc";
LPTSTR pStr=csStr.GetBuffer(10);
strcpy(pStr,"12345");
cout<<csStr.GetLength(); //3(錯誤的用法)
csStr.ReleaseBuffer();
cout<<csStr.GetLength(); //5(正確)
pStr=NULL;
//CString物件的任何方法都應在ReleaseBuffer之後呼叫
LPTSTR GetBufferSetLength( int nNewLength );
//申請新的空間,並返回指標
csStr="abc";
csStr.GetBufferSetLength(20);
cout<<csStr; //abc
count<<csStr.GetLength(); //20;
csStr.ReleaseBuffer();
count<<csStr.GetLength(); //3;
//使用GetBufferSetLength後可以不必使用ReleaseBuffer。
轉自:https://www.cnblogs.com/Caiqinghua/archive/2009/02/16/1391190.html