C++中char cstring string互相转换

1.CString字符串连接,避免用char* strcpy这样的方式好的多.

CString World(“World”);

CString HelloWorld = Hello + World;

2.格式化字符串

CString s;

s.Format(_T(“%d”),IntVar);

_T(x)代表让字符有unicode兼容性

3:Cstring 到 char* 类型的相互转化.

CString HelloWorld = CString(“Hello”) + CString(“World”);

4:char* 转化为CString

char * p = “This is a test”;

TCHAR * p = _T(“This is a test”);

CString Hello(“Hello”);

5:CString转化char*

CString对象包含三个值,一个指向缓冲区的指针,一个该缓冲中有效的字符计数以及一个缓冲区的长度.

LPCTSTR操作符(或者更明确地说就是const TCHAR*操作符)在CString类中被重载了.

CString s(“Hello World”);

LPCTSTR p = s;

可以转化:

CString s(_T(“HelloWorld”));

LPTSTR p = s.GetBuffer();

if(p != NULL)

*p = _T(\0);

s.ReleaseBuffer();

在GetBuffer和ReleaseBuffer方法之间,不能调用CString对象任何方法,因为这样对象将无法保证完整性.

posted on 2011-06-02 17:49  sunliming  阅读(1504)  评论(1编辑  收藏  举报