CString类函数详解(转载)
CString::Compare
int Compare( LPCTSTR lpsz ) const;
返回值字符串⼀样返回0
⼩于lpsz 返回-1
⼤于lpsz 返回1
区分⼤⼩字符
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
不区分⼤⼩字符
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是⼀次删除⼏个字符。根据我实验得出的结果:当nCount>要删
除字符串的最⼤长度(GetCount() - nIndex)时会出错,当nCount过⼤,没有⾜够的字符删除时,此函数不执⾏。
例⼦
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)就会执⾏错误
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" );
库函数printf详解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的字符,你可以把字符串理解为⼀个数组,GetAt类似于[].注意nIndex的范围,如果不合适会有调试错误。CString::GetBuffer
LPTSTR GetBuffer( int nMinBufLength );
返回值
⼀个指向对象的(以空字符结尾的)字符缓冲区的LPTSTR 指针。
参数
nMinBufLength
字符缓冲区的以字符数表⽰的最⼩容量。这个值不包括⼀个结尾的空字符的空间。
说明
此成员函数返回⼀个指向CString 对象的内部字符缓冲区的指针。返回的LPTSTR 不是const,因此可以允许直接修改CString 的内容。如果你使⽤由GetBuffer 返回的指针来改变字符串的内容,你必须在使⽤其它的CString 成员函数之前调⽤ReleaseBuffer 函数。
在调⽤ReleaseBuffer 之后,由GetBuffer 返回的地址也许就⽆效了,因为其它的CString 操作可能会导致CString 缓冲区被重新分配。如果你没有改变此CString 的长度,则缓冲区不会被重新分配。当此CString 对象被销毁时,其缓冲区内存将被⾃动释放。
注意,如果你⾃⼰知道字符串的长度,则你不应该添加结尾的空字符。但是,当你⽤ReleaseBuffer 来释放该缓冲区时,你必须指定最后的字符串长度。如果你添加了结尾的空字符,你应该给ReleaseBuffer 的长度参数传递-1 ,ReleaseBuffer 将对该缓冲区执⾏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::GetLength
int GetLength( ) const;
返回值
返回字符串中的字节计数。
说明
此成员函数⽤来获取这个CString 对象中的字节计数。这个计数不包括结尾的空字符。
对于多字节字符集(MBCS),GetLength 按每⼀个8 位字符计数;即,在⼀个多字节字符中的开始和结尾字节被算作两个字节。⽰例
下⾯的例⼦说明了如何使⽤CString::GetLength。
// CString::GetLength ⽰例
CString s( "abcdef" );
ASSERT( s.GetLength() == 6 );
CString::Insert
int Insert( int nIndex, TCHAR ch );
int Insert( int nIndex, LPCTSTR pstr );
返回修改后的长度,nIndex是字符(或字符串)插⼊后的索引号例⼦
CString str( “HockeyBest”);
int n = str.Insert( 6, “is” );
ASSERT( n == str.GetLength( ) );
printf( “1: %s\n”, ( LPCTSTR ) str );
n = str.Insert( 6, ' ' );
ASSERT( n == str.GetLength( ) );
printf ( “2: %s\n”, (LPCTSTR) STR );
n = str.Insert(555, ‘1’);
ASSERT( n == str.GetLength ( ) );
printf ( “3: %s\n”, ( LPCTSTR ) str );
输出
1. Hockeyis Best
2. Hockey is Best
3. Hockey is Best!
CString::IsEmpty
BOOL IsEmpty( ) const;
返回值
如果CString 对象的长度为0,则返回⾮零值;否则返回0。
说明
此成员函数⽤来测试⼀个CString 对象是否是空的。
⽰例
下⾯的例⼦说明了如何使⽤CString::IsEmpty。
// CString::IsEmpty ⽰例
CString s;
ASSERT( s.IsEmpty() );
请参阅 CString::GetLength
CString::Left
CString Left( int nCount ) const;
throw( CMemoryException );
返回的字符串是前nCount个字符。
例⼦
CString s( _T("abcdef") );
ASSERT( s.Left(2) == _T("ab") );
CString::LoadString
BOOL LoadString( UINT nID );
throw( CMemoryException );
返回值
如果加载资源成功则返回⾮零值;否则返回0。
nID ⼀个Windows 字符串资源ID。
说明此成员函数⽤来读取⼀个由nID 标识的Windows 字符串资源,并放⼊⼀个已有CString 对象中。
⽰例
下⾯的例⼦说明了如何使⽤CString::LoadString。
// CString::LoadString ⽰例
#define IDS_FILENOTFOUND 1
CString s;
if (! s.LoadString( IDS_FILENOTFOUND ))
CString::MakeLower
void MakeLower( );
改变字符的⼩写
CString::MakeReverse
void MakeReverse( );
字符倒置
CString::MakeUpper
void MakeUpper( );
改变字符的⼤写
CString::Mid
CString Mid( int nFirst ) const;
CString Mid( int nFirst, int nCount ) const;
nCount代表要提取的字符数, nFirst代表要提取的开始索引位置
例⼦
CString s( _T("abcdef") );
ASSERT( s.Mid( 2, 3 ) == _T("cde") );
CString::ReleaseBuffer
void ReleaseBuffer( int nNewLength = -1 );
参数
nNewLength
此字符串的以字符数表⽰的新长度,不计算结尾的空字符。如果这个字
符串是以空字符结尾的,则参数的缺省值-1 将把CString 的⼤⼩设置为
字符串的当前长度。
说明
使⽤ReleaseBuffer 来结束对由GetBuffer 分配的缓冲区的使⽤。如果你知道缓冲区中的字符串是以空字符结尾的,则可以省略nNewLength 参数。如果字符串不是以空字符结尾的,则可以使⽤nNewLength 指定字符串的长度。在调⽤ReleaseBuffer 或其它CString 操作之后,由GetBuffer 返回的地址是⽆效的。⽰例
下⾯的例⼦说明了如何使⽤CString::ReleaseBuffer。
/
/ CString::ReleaseBuffer ⽰例
CString s;
s = "abc";
LPTSTR p = s.GetBuffer( 1024 );
strcpy(p, "abc"); // 直接使⽤该缓冲区
ASSERT( s.GetLength() == 3 ); // 字符串长度 = 3
s.ReleaseBuffer(); // 释放多余的内存,现在p ⽆效。
ASSERT( s.GetLength() == 3 ); // 长度仍然是3
CString::Remove
int CString::Remove ( TCHAR ch );
返回值
返回从字符串中移⾛的字符数。如果字符串没有改变则返回零。
参数
ch
要从⼀个字符串中移⾛的字符。
说明
此成员函数⽤来将ch 实例从字符串中移⾛。与这个字符的⽐较是区分⼤⼩写的。
⽰例
// 从⼀个句⼦中移⾛⼩写字母'c':
CString str (“This is a test.”);
int n = str.Remove( 't' );
ASSERT( n == 2 );
ASSERT( str ==“This is a es. ” );
CString::Replace
int Replace( TCHAR chOld, TCHAR chNew );
int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );
返回值
返回被替换的字符数。如果这个字符串没有改变则返回零。
参数
chOld
要被chNew 替换的字符
chNew
要⽤来替换chOld 的字符。
lpszOld
⼀个指向字符串的指针,该字符串包含了要被lpszNew 替换的字符。lpszNew
⼀个指向字符串的指针,该字符串包含了要⽤来替换lpszOld 的字符。
说明
此成员函数⽤⼀个字符替换另⼀个字符。函数的第⼀个原形在字符串中⽤chNew 现场替换chOld。函数的第⼆个原形⽤lpszNew 指定的字符串替换lpszOld 指定的⼦串。
在替换之后,该字符串有可能增长或缩短;那是因为lpszNew 和lpszOld 的长度不需要是相等的。两种版本形式都进⾏区分⼤⼩写的匹配。