vs中CString的用法,以及所需的头文件

1、CString类型的头文件
#include <afx.h>
2、CString的输出
CString temp="Hello!";
cout<<(LPCTSTR)temp<<endl;
3、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" );
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

转自:http://hi.baidu.com/bbontime/item/fdbac0ba1cdd16ea4fc7fd31

  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值