CString类型的头文件、CString的输出、CString的常用用法
1、CString類型的頭文件
?????#include <afx.h>
?
2、CString的輸出
?????CString temp="Hello!";
?????cout<<(LPCTSTR)temp<<endl;
?
3、CString的用法
CString::Compare
int Compare( LPCTSTR lpsz ) const;
返回值 字符串一樣 返回0?
?????????小于lpsz 返回-1
?????????大于lpsz 返回1
?????????區(qū)分大小字符
?????????CString s1( "abc" );
CString s2( "abd" );
ASSERT( s1.Compare( s2 ) == -1 );
ASSERT( s1.Compare( "abe" ) == -1 );
CString::CompareNoCase
int CompareNoCase( LPCTSTR lpsz ) const;
返回值 字符串一樣 返回0?
????????小于lpsz 返回-1
????????大于lpsz 返回1
????????不區(qū)分大小字符
CString::Collate
int Collate( LPCTSTR lpsz ) const;
同CString::Compare
CString::CollateNoCase
int CollateNocase( LPCTSTR lpsz ) const;
同CString::CompareNoCase
CString::CString
CString( );
CString( const CString& stringSrc );?
CString( TCHAR ch, int nRepeat = 1 );?
CString( LPCTSTR lpch, int nLength );?
CString( const unsigned char* psz );?
CString( LPCWSTR lpsz );
CString( LPCSTR lpsz );?
例子最容易說明問題
CString s1;??????????????????????
CString s2( "cat" );???????????????
CString s3 = s2;??????????????????
CString s4( s2 + " " + s3 );?????????
CString s5( 'x' );??????????????????????// s5 = "x"
CString s6( 'x', 6 );???????????????????// s6 = "xxxxxx"
CString s7((LPCSTR)ID_FILE_NEW);????????// s7 = "Create a new document"
CString city = "Philadelphia";?
CString::Delete
int Delete( int nIndex, int nCount = 1);?
返回值是被刪除前的字符串的長度
nIndex是第一個被刪除的字符,nCount是一次刪除幾個字符。根據(jù)我實驗得出的結果:當nCount>要刪除字符串的最大長度(GetCount() - nIndex)時會出錯,當nCount過大,沒有足夠的字符刪除時,此函數(shù)不執(zhí)行。
例子
CString str1,str2,str3;
char a;
str1 = "nihao";
str2 = "nIhao";
int x;
// int i=(str1 == str2);???????
str1.Delete(2,3);
如果nCount(3) > GetCount() – nIndex (5-2)就會執(zhí)行錯誤
CString::Empty
Void Empty( );
沒有返回值 清空操作;
例子
CString s( "abc" );
s.Empty();
ASSERT( s.GetLength( ) == 0 );
CString::Find
int Find( TCHAR ch ) const;
int Find( LPCTSTR lpszSub ) const;
int Find( TCHAR ch, int nStart ) const;
int Find( LPCTSTR lpszSub, int nStart ) const;
返回值 不匹配的話返回 -1; 索引以0 開始
????????nStar 代表以索引值nStart 的字符開始搜索 ,
即為包含以索引nStart字符后的字符串
例子
CString s( "abcdef" );
ASSERT( s.Find( 'c' ) == 2 );
ASSERT( s.Find( "de" ) == 3 );
Cstring str(“The stars are aligned”);
Ing n = str.Find('e',5);
ASSERT(n == 12)
CString::FindOneOf
int FindOneOf( LPCTSTR lpszCharSet ) const;
返回值 不匹配的話返回 -1; 索引以0 開始
??????????注意::返回此字符串中第一個在lpszCharSet中 也包括字符并且從零開始的索引值
例子
CString s( "abcdef" );
ASSERT( s.FindOneOf( "xd" ) == 3 ); // 'd' is first match.
CString::Format
void Format( LPCTSTR lpszFormat, ... );
void Format( UINT nFormatID, ... );
lpszFormat 一個格式控制字符串
nFormatID 字符串標識符
例子
????????????CString str;
Str.Format(“%d”,13);?
此時Str為13
CString::GetAt
TCHAR GetAt( int nIndex ) const;
返回標號為nIndex的字符,你可以把字符串理解為一個數(shù)組,GetAt類似于[].注意nIndex的范圍,如果不合適會有調(diào)試錯誤。
CString::GetBuffer
LPTSTR GetBuffer( int nMinBufLength );
返回值
一個指向?qū)ο蟮?#xff08;以空字符結尾的)字符緩沖區(qū)的LPTSTR 指針。
參數(shù)
nMinBufLength
字符緩沖區(qū)的以字符數(shù)表示的最小容量。這個值不包括一個結尾的空字符的空間。
說明
此成員函數(shù)返回一個指向CString 對象的內(nèi)部字符緩沖區(qū)的指針。返回的LPTSTR 不是const,因此可以允許直接修改CString 的內(nèi)容。如果你使用由GetBuffer 返回的指針來改變字符串的內(nèi)容,你必須在使用其它的CString 成員函數(shù)之前調(diào)用ReleaseBuffer 函數(shù)。
在調(diào)用ReleaseBuffer 之后,由GetBuffer 返回的地址也許就無效了,因為其它的CString 操作可能會導致CString 緩沖區(qū)被重新分配。如果你沒有改變此CString 的長度,則緩沖區(qū)不會被重新分配。當此CString 對象被銷毀時,其緩沖區(qū)內(nèi)存將被自動釋放。
注意,如果你自己知道字符串的長度,則你不應該添加結尾的空字符。但是,當你用ReleaseBuffer 來釋放該緩沖區(qū)時,你必須指定最后的字符串長度。如果你添加了結尾的空字符, 你應該給ReleaseBuffer 的長度參數(shù)傳遞-1 ,ReleaseBuffer 將對該緩沖區(qū)執(zhí)行strlen 來確定它的長度。
下面的例子說明了如何用CString::GetBuffer。
// CString::GetBuffer 例子
CString s( "abcd" );
#ifdef _DEBUG
afxDump << "CString s " << s << "\n";
#endif
LPTSTR p = s.GetBuffer( 10 );
strcpy( p, "Hello" ); // 直接訪問CString 對象。
s.ReleaseBuffer( );
#ifdef _DEBUG
afxDump << "CString s " << s << "\n";
#endif
CString的用法出處:http://www.diybl.com/course/3_program/c++/cppjs/2007918/71880.html
轉(zhuǎn)載于:https://www.cnblogs.com/PierreDelatour/archive/2011/11/12/2246601.html
總結
以上是生活随笔為你收集整理的CString类型的头文件、CString的输出、CString的常用用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle -12169,很奇怪的错误
- 下一篇: NSDictionary和NSMutab