CString( ); 例:CString csStr; CString( const CString& stringSrc ); 例:CString csStr("ABCDEF中文123456"); CString csStr2(csStr); CString( TCHAR ch, int nRepeat = 1 ); 例:CString csStr('a',5); //csStr="aaaaa" CString( LPCTSTR lpch, int nLength ); 例:CString csStr("abcdef",3); //csStr="abc" CString( LPCWSTR lpsz ); 例:wchar_t s[]=L"abcdef"; CString csStr(s); //csStr=L"abcdef" CString( const unsigned char* psz ); 例:const unsigned char s[]="abcdef"; const unsigned char* sp=s; CString csStr(sp); //csStr="abcdef" CString( LPCSTR lpsz ); 例:CString csStr("abcdef"); //csStr="abcdef" 返回字符串的長度,不包含結尾的空字符。 例:csStr="ABCDEF中文123456"; printf("%d",csStr.GetLength()); //16 顛倒字符串的順序 例:csStr="ABCDEF中文123456"; csStr.MakeReverse(); cout< 將小寫字母轉換為大寫字母 例:csStr="abcdef中文123456"; csStr.MakeUpper(); cout< 將大寫字母轉換為小寫字母 例:csStr="ABCDEF中文123456"; csStr.MakeLower(); cout< 區分大小寫比較兩個字符串,相等時返回0,大於時返回1,小於時返回-1 例:csStr="abcdef中文123456"; csStr2="ABCDEF中文123456"; cout< 不區分大小寫比較兩個字符串,相等時返回0,大於時返回1,小於時返回-1 例:csStr="abcdef中文123456"; csStr2="ABCDEF中文123456"; cout< 刪除字符,刪除從下標nIndex開始的nCount個字符 例:csStr="ABCDEF"; csStr.Delete(2,3); cout< //當nIndex過大,超出對像所在內存區域時,函數沒有任何操作。 //當nIndex為負數時,從第一個字符開始刪除。 //當nCount過大,導致刪除字符超出對像所在內存區域時,會發生無法預料的結果。 //當nCount為負數時,函數沒有任何操作。 int Insert( int nIndex, LPCTSTR pstr ) 在下標為nIndex的位置,插入字符或字符串。返回插入後對象的長度 例:csStr="abc"; csStr.Insert(2,'x'); cout< csStr.Insert(2,"xyz"); cout< //當nIndex為負數時,插入在對象開頭 //當nIndex超出對象末尾時,插入在對象末尾 移除對象內的指定字符。返回移除的數目 例:csStr="aabbaacc"; csStr.Remove('a'); cout< int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew ); 替換字串 例:csStr="abcdef"; csStr.Replace('a','x'); cout< csStr="abcdef"; csStr.Replace("abc","xyz"); cout< void TrimLeft( TCHAR chTarget ); void TrimLeft( LPCTSTR lpszTargets ); 從左刪除字符,被刪的字符與chTarget或lpszTargets匹配,一直刪到第一個不匹配的字符為止 例:csStr="aaabaacdef"; csStr.TrimLeft('a'); cout< csStr.TrimLeft("ab"); cout< //無參數時刪除空格 void TrimRight( TCHAR chTarget ); void TrimRight( LPCTSTR lpszTargets ); 從右刪除字符,被刪的字符與chTarget或lpszTargets匹配,一直刪到第一個不匹配的字符為止 例:csStr="abcdeaafaaa"; csStr.TrimRight('a'); cout< csStr="abcdeaafaaa"; csStr.TrimRight("fa"); cout< //無參數時刪除空格 清空 例:csStr="abcdef"; csStr.Empty(); printf("%d",csStr.GetLength()); //0 測試對象是否為空,為空時返回零,不為空時返回非零 例:csStr="abc"; cout< cout< int Find( LPCTSTR lpszSub ) const; int Find( TCHAR ch, int nStart ) const; int Find( LPCTSTR pstr, int nStart ) const; 查找字串,nStart為開始查找的位置。未找到匹配時返回-1,否則返回字串的開始位置 例:csStr="abcdef"; cout< cout< cout< cout< cout< //當nStart超出對象末尾時,返回-1。 //當nStart為負數時,返回-1。 查找lpszCharSet中任意一個字符在CString對象中的匹配位置。未找到時返回-1,否則返回字串的開始位置 例:csStr="abcdef"; cout< 返回對象中與lpszCharSet中任意匹配的第一個字符之前的子串 例:csStr="abcdef"; cout< 從對象中查找與lpszCharSe中任意字符不匹配的字符,並返回第一個不匹配字符之前的字串 例:csStr="abcdef"; cout< 從後向前查找第一個匹配,找到時返回下標。沒找到時返回-1 例:csStr="abba"; cout< void Format( UINT nFormatID, ... ); 格式化對象,與C語言的sprintf函數用法相同 例:csStr.Format("%d",13); cout< 返回下標為nIndex的字符,與字符串的[]用法相同 例:csStr="abcdef"; cout< //當nIndex為負數或超出對象末尾時,會發生無法預料的結果。 給下標為nIndex的字符重新賦值 例:csStr="abcdef"; csStr.SetAt(2,'x'); cout< //當nIndex為負數或超出對象末尾時,會發生無法預料的結果。 從左取字串 例:csStr="abcdef"; cout< //當nCount等於0時,返回空。 //當nCount為負數時,返回空。 //當nCount大於對象長度時,返回值與對象相同。 從右取字串 例:csStr="abcdef"; cout< //當nCount等於0時,返回空。 //當nCount為負數時,返回空。 //當nCount大於對象長度時,返回值與對象相同。 CString Mid( int nFirst, int nCount ) const; 從中間開始取字串 例:csStr="abcdef"; cout< cout< //當nFirst為0和為負數時,從第一個字符開始取。 //當nFirst等於對象末尾時,返回空字串。 //當nFirst超出對象末尾時,會發生無法預料的結果。 //當nCount超出對象末尾時,返回從nFirst開始一直到對象末尾的字串 //當nCount為0和為負數時,返回空字串。 申請新的空間,並返回指針 例:csStr="abcde"; LPTSTR pStr=csStr.GetBuffer(10); strcpy(pStr,"12345"); csStr.ReleaseBuffer(); pStr=NULL; cout< //使用完GetBuffer後,必須使用ReleaseBuffer以更新對象內部數據,否則會發生無法預料的結果。 使用GetBuffer後,必須使用ReleaseBuffer以更新對象內部數據 例:csStr="abc"; LPTSTR pStr=csStr.GetBuffer(10); strcpy(pStr,"12345"); cout< csStr.ReleaseBuffer(); cout< //CString對象的任何方法都應在ReleaseBuffer之後調用 申請新的空間,並返回指針 例:csStr="abc"; csStr.GetBufferSetLength(20); cout< count< //使用GetBufferSetLength後可以不必使用ReleaseBuffer。 |
2012年3月26日 星期一
CString的構造函數
訂閱:
文章 (Atom)