1. 简介

一个CString对象由可变长度的字符数组构造,由PXSTR m_pszData成员变量维护对象的地址信息。CString对象可以任意替换const char*和LPCTSTR类型的函数参数,而不需要强制转换。

2. 构造函数

Cstring构造函数有多种形式,常见的声明如下:

  1. CString( );
  2. CString( const CString& stringSrc );
  3. CString( const unsigned char* psz );
  4. CString( LPCSTR lpsz );


例如:

  1. //无参构造函数
  2. CString str1;
  3. //拷贝构造
  4. CString str2(str1);
  5. CString str3 = str1;
  6. CString str4 = "cstring obj";
  7. CString str5("cstring obj");
  8. TCHAR szObj[] = "cstring obj";
  9. 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 比较操作符系列
作用:比较两个字符串是否相等,大小写敏感

  1. BOOL operator ==( const CString& s1, const CString& s2 );
  2. BOOL operator ==( const CString& s1, LPCTSTR s2 );
  3. BOOL operator ==( LPCTSTR s1, const CString& s2 );
  1. //作用:比较两个字符串,大小写敏感;相等则返回0,我们可以用operator操作符替代Compare函数.
  2. int Compare( LPCTSTR lpsz ) const;
  1. //作用:比较两个字符串是否相等,大小写不敏感,相关等则返回值为0;
  2. int CompareNoCase( LPCTSTR lpsz ) const;

例如:

  1. CString s1( "abc" );
  2. CString s2( "abd" );
  3. // Operator is overloaded for both.
  4. ASSERT( s1 < s2 );
  5. // CString and char*
  6. ASSERT( "ABC" < s1 );
  7. ASSERT( s2 > "abe" );
  8. CString s1( "abc" );
  9. CString s2( "ABD" );
  10. // 与一个CString比较。
  11. ASSERT( s1.CompareNoCase( s2 ) == -1 );
  12. // 与LPTSTR字符串比较。
  13. 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个字符。

  1. CString s( _T("abcdef") );
  2. ASSERT( s.Mid( 2, 3 ) == _T("cde") );
  3. CString s( _T("abcdef") );
  4. ASSERT( s.Left(2) == _T("ab") );
  5. CString s( _T("abcdef") );
  6. ASSERT( s.Right(2) == _T("ef") );

6. 其他转换

  1. void TrimRight( );
  2. void CString::TrimRight( TCHAR chTarget );
  3. void CString::TrimRight( LPCTSTR lpszTargets );
  4. void TrimLeft( );
  5. void CString::TrimLeft( TCHAR chTarget );
  6. void CString::TrimLeft( LPCTSTR lpszTargets );

作用:
无参的版本剔除最右边(最左边)的空格,Tab键,换行符;
有参版本剔除指定的字符或者字符串或者子串,若有连续的多个被剔除字符,可以继续被剔除;想同时删除最左边,最右边,可以使用Trim函数,它是TrimLeft和TrimRight的集合。

例如:

  1. CString strBefore("!@#!@123!@#");
  2. CString strAfter = strBefore;
  3. strAfter.TrimLeft("!@#") ;
  4. printf ("Before: \"%s\"\n",(LPCTSTR) strBefore );
  5. printf ("After: \"%s\"\n",(LPCTSTR) strAfter );
  6. 结果:字符串“!@#!@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;
作用:
在此字符串中搜索指定的字符或者字符串。

例如:

  1. // CString::Find( TCHAR ch )
  2. CString s( "abcdef" );
  3. ASSERT( s.Find( 'c' ) == 2 );
  4. ASSERT( s.Find( "de" ) == 3 );
  5. // CString::Find(TCHAR ch,int nStart)
  6. CString str("The stars are aligned");
  7. int n = str.Find('e',5);
  8. ASSERT(n == 12)


int ReverseFind( TCHAR ch ) const;
作用:
在字符串中反向查找匹配的ch字符
例如:

  1. CString s( "abcabc" );
  2. 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返回的地址也许就无效了.
例如:

  1. CString s;
  2. s = "abc";
  3. LPTSTR p = s.GetBuffer(1024);
  4. // 直接使用该缓冲区
  5. strcpy(p, "abc");
  6. // 字符串长度 = 3
  7. ASSERT( s.GetLength() == 3 );
  8. // 释放多余的内存,现在p无效。
  9. s.ReleaseBuffer();
  10. // 长度仍然是3
  11. ASSERT( s.GetLength() == 3 );