C++—CString用法大全

发表于:2014-12-18 09:29

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:农村的我    来源:51Testing软件测试网采编

  例:csStr="abcdef中文123456";
  csStr2="ABCDEF中文123456";
  cout<<csStr.CompareNoCase(csStr2); //-1
  int Delete( int nIndex, int nCount = 1 )
  删除字符,删除从下标nIndex开始的nCount个字符
  例:csStr="ABCDEF";
  csStr.Delete(2,3);
  cout<<csStr; // ABF
  //当nIndex过大,超出对像所在内存区域时,函数没有任何操作。
  //当nIndex为负数时,从第一个字符开始删除。
  //当nCount过大,导致删除字符超出对像所在内存区域时,会发生无法预料的结果。
  //当nCount为负数时,函数没有任何操作。
  int Insert( int nIndex, TCHAR ch )
  int Insert( int nIndex, LPCTSTR pstr )
  在下标为nIndex的位置,插入字符或字符串。返回插入后对象的长度
  例:csStr="abc";
  csStr.Insert(2,'x');
  cout<<csStr; //abxc
  csStr="abc";
  csStr.Insert(2,"xyz");
  cout<<csStr; //abxyzc
  //当nIndex为负数时,插入在对象开头
  //当nIndex超出对象末尾时,插入在对象末尾
  int Remove( TCHAR ch );
  移除对象内的指定字符。返回移除的数目
  例:csStr="aabbaacc";
  csStr.Remove('a');
  cout<<csStr; //bbcc
  int Replace( TCHAR chOld, TCHAR chNew );
  int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );
  替换字串
  例:csStr="abcdef";
  csStr.Replace('a','x');
  cout<<csStr; //xbcdef
  csStr="abcdef";
  csStr.Replace("abc","xyz");
  cout<<csStr; //xyzdef
  void TrimLeft( );
  void TrimLeft( TCHAR chTarget );
  void TrimLeft( LPCTSTR lpszTargets );
  从左删除字符,被删的字符与chTarget或lpszTargets匹配,一直删到第一个不匹配的字符为止
  例:csStr="aaabaacdef";
  csStr.TrimLeft('a');
  cout<<csStr; //baacdef
  csStr="aaabaacdef";
  csStr.TrimLeft("ab");
  cout<<csStr; //cdef
  //无参数时删除空格
  void TrimRight( );
  void TrimRight( TCHAR chTarget );
  void TrimRight( LPCTSTR lpszTargets );
  从右删除字符,被删的字符与chTarget或lpszTargets匹配,一直删到第一个不匹配的字符为止
  例:csStr="abcdeaafaaa";
  csStr.TrimRight('a');
  cout<<csStr; //abcdeaaf
  csStr="abcdeaafaaa";
  csStr.TrimRight("fa");
  cout<<csStr; //abcde
  //无参数时删除空格
  void Empty( );
  清空
  例:csStr="abcdef";
  csStr.Empty();
  printf("%d",csStr.GetLength()); //0
  BOOL IsEmpty( ) const;
  测试对象是否为空,为空时返回零,不为空时返回非零
  例:csStr="abc";
  cout<<csStr.IsEmpty(); //0;
  csStr.Empty();
  cout<<csStr.IsEmpty(); //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;
  查找字串,nStart为开始查找的位置。未找到匹配时返回-1,否则返回字串的开始位置
  例:csStr="abcdef";
  cout<<csStr.Find('b'); //1
  cout<<csStr.Find("de"); //3
  cout<<csStr.Find('b',3); //-1
  cout<<csStr.Find('b',0); //1
  cout<<csStr.Find("de",4); //-1
  cout<<csStr.Find("de",0); //3
  //当nStart超出对象末尾时,返回-1。
  //当nStart为负数时,返回-1。
  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;
  从中间开始取字串
  例:csStr="abcdef";
  cout<<csStr.Mid(2); //cdef
  csStr="abcdef";
  cout<<csStr.Mid(2,3); //cde
  //当nFirst为0和为负数时,从第一个字符开始取。
  //当nFirst等于对象末尾时,返回空字串。
  //当nFirst超出对象末尾时,会发生无法预料的结果。
  //当nCount超出对象末尾时,返回从nFirst开始一直到对象末尾的字串
  //当nCount为0和为负数时,返回空字串。
  LPTSTR GetBuffer( int nMinBufLength );
  申请新的空间,并返回指针
  例:csStr="abcde";
  LPTSTR pStr=csStr.GetBuffer(10);
  strcpy(pStr,"12345");
  csStr.ReleaseBuffer();
  pStr=NULL;
  cout<<csStr //12345
  //使用完GetBuffer后,必须使用ReleaseBuffer以更新对象内部数据,否则会发生无法预料的结果。
  void ReleaseBuffer( int nNewLength = -1 );
42/4<1234>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号