MFC CString类

// CString 详解


// 构造函数
CString (const CString& src)
CString str("ABC123");
CString str(src);


// 用字符初始化
CString(TCHAR ch, int nReapt)
CString str('a', 5); // str = "aaaaaa";


// 用字符串前几个字符初始化
CString(LPCTSTR lpch, int nLength)
CString str("abc123", 3); // str = "abc";


//用宽字符串初始化
CString (LPCWSTR lpsz)
wchar_t s[] = L"abc123";
CString str(s); //str = L"abc123"


// 用字符串初始化
CString(LPCSTR lpsz)           
CString csStr("abc123"); // csStr = "abc123"


// 用字符串初始化
CString(const unsigned char * psz)  
const unsigned char s[] = "abc123";
const unsigned char *sp = s;
CString str(sp); // csStr = "abcd123"  


// ------------------------------------//


//返回字符串的长度, 不包含结尾空字符
int GetLength() const;


// 颠倒字符串顺序
void MakeRevese();


// 小字母 大字母 互转
void MakeUpper();
void MakeLower();


// 区分大小写 比较两个字符串, 相等返回0; 大于返回1, 小于返回-1 
int Compare(LPCTSTR lpsz) const;
// 不区分大小写
int CompareNoCase(LPCTSTR lpsz) const;


// 删除字符, 从下标nIndex 开始的 nCount 个字符
int Delete(int nIndex, int nCount = 1)


// 在下标为 nIndex 的位置插入字符(串), 返回插入后对象的长度
int Insert(int nIndex, TCHAR ch)
int Insert(int nIndex, LPCTSTR pstr)
//例:
      csStr="abc"; 
      csStr.Insert(2,'x'); 
      cout<<csStr;                    
      //abxc 
      csStr="abc"; 
      csStr.Insert(2,"xyz"); 
      cout<<csStr;                    
      //abxyzc
 
      //当nIndex为负数时,插入在对象开头
      //当nIndex超出对象末尾时,插入在对象末尾


// 移除对象的指定字符, 返回移除的数目
int Remove(TCHAR ch);


// 替换字符(c串) 
int Replace(TCHAR chOld, TCHAR chNew)
int Replace(LPCTSTR lpszOld, LPCTSTR lpszNew)




//从左删除字符,被删的字符与chTarget或lpszTargets匹配,
//一直删到第一个不匹配的字符为止
void TrimLeft( ); 
void TrimLeft( TCHAR chTarget ); 
void TrimLeft( LPCTSTR lpszTargets );


//从右删除字符,被删的字符与chTarget或lpszTargets匹配,
//一直删到第一个不匹配的字符为止
void TrimRight( ); 
void TrimRight( TCHAR chTarget ); 
void TrimRight( LPCTSTR lpszTargets );




void Empty();


BOOL IsEmpty() const;


//查找字串,nStart为开始查找的位置。
//未找到匹配时返回-1,否则返回字串的开始位置
int Find( TCHAR ch ) const; 
int Find( LPCTSTR lpszSub ) const; 
int Find( TCHAR ch, int nStart ) const; 
int Find( LPCTSTR pstr, int nStart ) const;




int FindOneOf( LPCTSTR lpszCharSet ) const;
//查找lpszCharSet中任意一个字符在CString对象中的匹配位置。
//未找到时返回-1,否则返回字串的开始位置
//例:
      csStr="abcdef"; 
      cout<<csStr.FindOneOf("cxy");      
      //2
 
 
CString SpanExcluding( LPCTSTR lpszCharSet ) const;
//返回对象中与lpszCharSet中任意匹配的第一个字符之前的子串
//例:
      csStr="abcdef"; 
      cout<<csStr.SpanExcluding("cf");    
      //ab
 
 
CString SpanIncluding( LPCTSTR lpszCharSet ) const;
//从对象中查找与lpszCharSe中任意字符不匹配的字符,
//并返回第一个不匹配字符之前的字串 
//例:
      csStr="abcdef"; 
      cout<<csStr.SpanIncluding("fdcba");    
      //abcd
 
 
int ReverseFind( TCHAR ch ) const;
//从后向前查找第一个匹配,找到时返回下标。没找到时返回-1
//例:
      csStr="abba"; 
      cout<<csStr.ReverseFind('a');        
      //3


 
void Format( LPCTSTR lpszFormat, ... ); 
void Format( UINT nFormatID, ... );
//格式化对象,与C语言的sprintf函数用法相同
//例:
      csStr.Format("%d",13); 
      cout<<csStr;                       
      //13
 


TCHAR GetAt( int nIndex ) const;
//返回下标为nIndex的字符,与字符串的[]用法相同
//例:
      csStr="abcdef"; 
      cout<<csStr.GetAt(2);             
      //c 
      //当nIndex为负数或超出对象末尾时,会发生无法预料的结果。
 
 
void SetAt( int nIndex, TCHAR ch );
//给下标为nIndex的字符重新赋值
//例:
      csStr="abcdef"; 
      csStr.SetAt(2,'x'); 
      cout<<csStr;                      
      //abxdef 
      //当nIndex为负数或超出对象末尾时,会发生无法预料的结果。
 
 
CString Left( int nCount ) const;
//从左取字串
//例:
      csStr="abcdef"; 
      cout<<csStr.Left(3);          
      //abc 
//当nCount等于0时,返回空。
//当nCount为负数时,返回空。
//当nCount大于对象长度时,返回值与对象相同。
 
 
CString Right( int nCount ) const;
//从右取字串
//例:
      csStr="abcdef"; 
      cout<<csStr.Right(3);           
      //def 
//当nCount等于0时,返回空。
//当nCount为负数时,返回空。
//当nCount大于对象长度时,返回值与对象相同。
 
 
CString Mid( int nFirst ) const; 
CString Mid( int nFirst, int nCount ) const;
//从中间开始取字串
//当nFirst为0和为负数时,从第一个字符开始取。
//当nFirst等于对象末尾时,返回空字串。
//当nFirst超出对象末尾时,会发生无法预料的结果。
//当nCount超出对象末尾时,返回从nFirst开始一直到对象末尾的字串
//当nCount为0和为负数时,返回空字串。 
//例:
      csStr="abcdef"; 
      cout<<csStr.Mid(2);           
      //cdef 
      csStr="abcdef"; 
      cout<<csStr.Mid(2,3);         
      //cde 




LPTSTR GetBuffer( int nMinBufLength );
//申请新的空间,并返回指针
//使用完GetBuffer后,必须使用ReleaseBuffer以更新对象内部数据,
//否则会发生无法预料的结果。
//例:
      csStr="abcde"; 
      LPTSTR pStr=csStr.GetBuffer(10); 
      strcpy(pStr,"12345"); 
      csStr.ReleaseBuffer(); 
      pStr=NULL; 
      cout<<csStr;     
      //12345 




void ReleaseBuffer( int nNewLength = -1 );
//使用GetBuffer后,必须使用ReleaseBuffer以更新对象内部数据
//例:
      csStr="abc"; 
      LPTSTR pStr=csStr.GetBuffer(10); 
      strcpy(pStr,"12345"); 
      cout<<csStr.GetLength();       
      //3(错误的用法)
 
      csStr.ReleaseBuffer(); 
      cout<<csStr.GetLength();       
      //5(正确)


      pStr=NULL; 
      //CString 对象的任何方法都应在ReleaseBuffer之后调用
 


LPTSTR GetBufferSetLength( int nNewLength );
//申请新的空间,并返回指针
//例:
      csStr="abc"; 
      csStr.GetBufferSetLength(20); 
      cout<<csStr;                  
      //abc 
      count<<csStr.GetLength();    
      //20; 
      csStr.ReleaseBuffer(); 
      count<<csStr.GetLength();     
      //3; 
//使用GetBufferSetLength后可以不必使用ReleaseBuffer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值