大家好,欢迎来到IT知识分享网。
CString详解
1、成员函数
| 函数成员 | 函数功能 |
|---|---|
| int GetLength( ) const | 返回字符串的长度,不包含结尾的空字符 |
| void MakeReverse( ) | 颠倒字符串的顺序 |
| void MakeUpper( ) | 将小写字母转换为大写字母 |
| void MakeLower( ) | 将大写字母转换为小写字母 |
| int Delete( int nIndex, int nCount = 1 ) | 删除从下标nIndex开始的nCount个字符 |
| int Insert( int nIndex, TCHAR ch ); int Insert( int nIndex, LPCTSTR pstr ) | 在下标为nIndex的位置,插入字符或字符串。返回插入后对象的长度 |
| int Remove( TCHAR ch ) | 移除对象内的指定字符。返回移除的数目 |
| int Replace( TCHAR chOld, TCHAR chNew ); int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew ) | 替换字符或字串 |
| void Empty( ) | 清空 |
| BOOL IsEmpty( ) const | 判断测试对象是否为空,为空时返回true,不为空时返回false |
| 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,否则返回字串的开始位置 |
| int FindOneOf( LPCTSTR lpszCharSet ) const | 查找lpszCharSet中任意一个字符在CString对象中的匹配位置。未找到时返回-1,否则返回字串的开始位置 |
| void Format( LPCTSTR lpszFormat, … );void Format( UINT nFormatID, … ) | 格式化成字符串,与C语言的sprintf函数用法相同 |
| TCHAR GetAt( int nIndex ) const | 返回下标为nIndex的字符,与字符串的[]用法相同 |
| void SetAt( int nIndex, TCHAR ch ) | 给下标为nIndex的字符重新赋值 |
| CString Left( int nCount ) const | 从左取字串 |
| CString Right( int nCount ) const | 从右取字串 |
| LPTSTR GetBuffer( int nMinBufLength ) | 一个指向对象的(以空字符结尾的)字符缓冲区的LPTSTR指针 |
| void ReleaseBuffer( int nNewLength = -1 ) | 使用GetBuffer后,必须使用ReleaseBuffer以更新对象内部数据 |
| LPTSTR GetBufferSetLength( int nNewLength ) | 一个指向对象的(以空字符结尾的)字符缓冲区的LPTSTR指针,同时可以设置字符串的新长度 |
| operator + | 将两个字符串合并成一个新的字符串。在两个参数中必须有一个是CString类型的,而另一个参数可以是字符、字符指针或CString类型对象 |
| operator += | 在一个字符串的后面再添加一个字符串或一个字符 |
| operator = | 将一个新的值赋予CString对象 |
LPCTSTR:
LP代表长指针、 C代表不可改变、 T代表根据是否定义UNICODE宏而分别define为char或wchar_t、 STR代表字符串。 为了写程序方便微软定义了类型LPTSTR,在MBCS下它就是char*,在UNICODE下它是unsigned char*,这样你就可以重定义一个宏进行不同字符集的转换了。
2、数据类型转换
CString与string之间的转换
1、Cstring -> string
CString strCstrSource("Is a test"); string strTarget; strTarget = strCstrSource.GetBuffer(0);
2、 string->Cstring
string strSource = "Is a test"; CString strCstrTarget; strCstrTarget = strSource.c_str();
其他数据类型转换成Cstring
使用CString的成员函数Format来转换,例如:
//整数(int) str.Format("%d",i); //浮点数(float) str.Format("%f",i); //字符串指针(char *)等已经被CString构造函数支持的数据类型可以直接赋值,如: CString myString = “This is a test”; 也可以利用构造函数,如: CString s1(“Tom”);
Tips:使用CString的Format函数的时候,如果带入的转化字符串为string类型的,就需要先转化为CString,然后再带入,不然编译可以通过,但是运行的时候会在Format的时候崩溃。
CString类型的变量赋给char*类型的变量
1、GetBuffer函数:
char *p; CString str="hello"; p=str.GetBuffer(str.GetLength()); str.ReleaseBuffer();
CString类向const char *转换
char a[100]; CString str("aaaaaa"); strncpy(a,(LPCTSTR)str,sizeof(a));
或者如下:
strncpy(a,str,sizeof(a));
以上两种用法都是正确地. 因为strncpy的第二个参数类型为const char *.所以编译器会自动将CString类转换成const char *.
CString转LPCTSTR (const char *)
CString cStr; const char *lpctStr=(LPCTSTR)cStr;
LPCTSTR转CString
LPCTSTR lpctStr; CString cStr=lpctStr;
将CString类型的变量赋给char []类型(字符串)的变量
1、strncpy_s
char a[100]; CString str("aaaaaa"); strncpy_s(a,sizeof(a),(LPCTSTR)str,GetLength(str));
2、strcpy_s
char mychar[1024]; CString source="Hello"; strcpy_s(mychar,strlen(mychar)+1,(LPCTSTR)source);
CString mCS=_T("cxl"); char mch[20]; memcpy(mch,mCS,20);
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/117369.html