1. 简介
一个CString对象由可变长度的字符数组构造,由PXSTR m_pszData成员变量维护对象的地址信息。CString对象可以任意替换const char*和LPCTSTR类型的函数参数,而不需要强制转换。
2. 构造函数
Cstring构造函数有多种形式,常见的声明如下:
CString( );
CString( const CString& stringSrc );
CString( const unsigned char* psz );
CString( LPCSTR lpsz );
例如:
//无参构造函数
CString str1;
//拷贝构造
CString str2(str1);
CString str3 = str1;
CString str4 = "cstring obj";
CString str5("cstring obj");
TCHAR szObj[] = "cstring obj";
CString str6(szObj);
3. 字符串数组
int GetLength( ) const;
返回值:
返回字符串中的字节计数。
作用:
获取这个CString对象中的字节计数,不包括结尾的空字符。int GetLength( ) const;
返回值:
返回字符串中的字节计数。
作用:
获取这个CString对象中的字节计数,不包括结尾的空字符。BOOL IsEmpty( ) const;
返回值:
如果CString对象的长度为0,则返回非零值;否则返回0。
作用:
测试一个CString对象是否是空的。TCHAR GetAt( int nIndex ) const;
参数:
nIndex 位置从零开始
作用:
返回字符串中指定位置的字符void SetAt( int nIndex, TCHAR ch );
参数:
nIndex 指定位置
ch 指定字符
作用:
指定某个位置的字符
4. 比较
operator 比较操作符系列
BOOL operator ==( const CString& s1, const CString& s2 );
BOOL operator ==( const CString& s1, LPCTSTR s2 );
BOOL operator ==( LPCTSTR s1, const CString& s2 );
作用:比较两个字符串是否相等,大小写敏感int Compare( LPCTSTR lpsz ) const;
作用:比较两个字符串,大小写敏感;相等则返回0,我们可以用operator操作符替代Compare函数.int CompareNoCase( LPCTSTR lpsz ) const;
作用:比较两个字符串是否相等,大小写不敏感,相关等则返回值为0;
例如:
CString s1( "abc" );
CString s2( "abd" );
// Operator is overloaded for both.
ASSERT( s1 < s2 );
// CString and char*
ASSERT( "ABC" < s1 );
ASSERT( s2 > "abe" );
CString s1( "abc" );
CString s2( "ABD" );
// 与一个CString比较。
ASSERT( s1.CompareNoCase( s2 ) == -1 );
// 与LPTSTR字符串比较。
ASSERT( s1.Compare( "ABE" ) == -1 );
5. 提取
CString Mid( int nFirst ) const;
CString Mid( int nFirst, int nCount ) const;
作用:
从对象中提取一个长度为nCount个字符的子串,从nFirst指定的位置开始提取。CString Left( int nCount ) const;
作用:
从此CString对象中提取最左边的nCount个字符。CString Right( int nCount ) const;
作用:
从此CString对象中提取最右边的nCount个字符。
CString s( _T("abcdef") );
ASSERT( s.Mid( 2, 3 ) == _T("cde") );
CString s( _T("abcdef") );
ASSERT( s.Left(2) == _T("ab") );
CString s( _T("abcdef") );
ASSERT( s.Right(2) == _T("ef") );
6. 其他转换
void TrimRight( );
void CString::TrimRight( TCHAR chTarget );
void CString::TrimRight( LPCTSTR lpszTargets );void TrimLeft( );
void CString::TrimLeft( TCHAR chTarget );
void CString::TrimLeft( LPCTSTR lpszTargets );作用:
无参的版本剔除最右边(最左边)的空格,Tab键,换行符;
有参版本剔除指定的字符或者字符串或者子串,若有连续的多个被剔除字符,可以继续被剔除;想同时删除最左边,最右边,可以使用Trim函数,它是TrimLeft和TrimRight的集合。
例如:
CString strBefore("!@#!@123!@#");
CString strAfter = strBefore;
strAfter.TrimLeft("!@#") ;
printf ("Before: "%s"
",(LPCTSTR) strBefore );
printf ("After: "%s"
",(LPCTSTR) strAfter );
结果:字符串“!@#!@123!@#”变成了“123!@#”。
void MakeUpper( );
作用:将字符串中的字符全部变为大写字符void MakeLower( );
作用:将字符串中的字符全部变为小写字符void MakeReverse( );
作用:将字符串中的字符以倒序排列int Replace( TCHAR chOld, TCHAR chNew );
int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );
作用:用其它字符替换指定的字符int Remove ( TCHAR ch );
作用:从一个字符串中移走指定的字符int Insert( int nIndex, TCHAR ch );
int Insert( int nIndex, LPCTSTR pstr );
作用:在字符串中的给定索引处插入一个字符或一个子字符串
7. 搜索
int Find( TCHAR ch ) const;
int Find( LPCTSTR lpszSub ) const;
int Find( LPCTSTR lpszSub, int nStart ) const;
作用:
在此字符串中搜索指定的字符或者字符串。例如:
// CString::Find( TCHAR ch )
CString s( "abcdef" );
ASSERT( s.Find( 'c' ) == 2 );
ASSERT( s.Find( "de" ) == 3 );
// CString::Find(TCHAR ch,int nStart)
CString str("The stars are aligned");
int n = str.Find('e',5);
ASSERT(n == 12)
- int ReverseFind( TCHAR ch ) const;
作用:
在字符串中反向查找匹配的ch字符
例如:
CString s( "abcabc" );
ASSERT( s.ReverseFind( 'b' ) == 4 );
8. 缓冲区访问
LPTSTR GetBuffer( int nMinBufLength );
作用:返回一个指向CString对象的内部字符缓冲区的指针(m_pszData),nMinBufLength决定Buffer大小。void ReleaseBuffer( int nNewLength = -1 )
作用:释放对GetBuffer所返回的缓冲区的控制权。
说明:
GetBuffer返回一个指向CString对象的内部字符缓冲区的指针。返回的LPTSTR不是const,因此可以允许直接修改CString的内容。
如果使用由GetBuffer返回的指针来改变字符串的内容,你必须在使用其它的CString成员函数之前调用ReleaseBuffer函数。
在调用ReleaseBuffer之后,由GetBuffer返回的地址也许就无效了.
例如:
CString s;
s = "abc";
LPTSTR p = s.GetBuffer(1024);
// 直接使用该缓冲区
strcpy(p, "abc");
// 字符串长度 = 3
ASSERT( s.GetLength() == 3 );
// 释放多余的内存,现在p无效。
s.ReleaseBuffer();
// 长度仍然是3
ASSERT( s.GetLength() == 3 );