CString
类学习笔记
2007-7-19
CString
类没有基本类,类的声明保存在
afx.h
头文件中。
一个
CString
对象是由可变长度的字符串组成,
CString
类提供了函数和操作符,内存管理,使用起来比字符数字容易的多。
CString
类基于
TCHAE
数据类型,如果你的程序采用
_UNICODE
字符集,则
TCHAR
将被定义为
16
位,
char
类型默认采用
8
位
CString
类的初始化:
CString();
声明一个
CString
对象,但没有初始化该对象。
CString(const CString& stringSrc);
Throw(CMemoryException);
该构造函数表示将一个已经存在的
CString
对象拷贝到声明的
CString
对象中。
CString(TCHAR ch, int nRepeat = 1);
throw(CMemoryException);
该构造函数表示将
ch
这个简单字符重复
nRepeat
遍来创建一个
CString
对象。
CString(LPCTSTR lpch , int nLength);
throw(CMemoryException);
构造一个长度为
nLength
的
CString
对象,该对象初始化值为一个字符串数组常量指针。
CString(const unsigned char* psz);
Throw(CMemoryException);
根据无符号字符指针创建
CString
对象。
CString(LPCWSTR lpsz);
throw(CMemoryException);
根据一个
unicode
字符串来创建一个
CString
对象。
CString(LPCSTR lpsz);
throw(CMemoryException);
根据一个
ansi
字符串来创建一个
unicode CString
对象。
作为数组的串:
int GetLength() const;
返回该串的字节数,该函数返回值不包括空结束符
”/0”
,如果该
CString
对象被声明成
unicode
字符集,则会按照一个字符两个字节来计算
CString
对象的长度。
BOOL IsEmpty() const;
如果
CString
对象的长度为
0
则返回为非
0
,否则返回
0
值。
void Empty();
将
CString
对象设置为空串并且相应的释放掉内存。
TCHAR GetAt(int nIndex) const;
返回该串特定位置的字符,
nIndex
是数组的下标,从
0
开始,注意
nIndex
的范围为
0
到
GetLength()-1
。
TCHAR
:
是为了统一多语言编码而设计的;
ANSI
-单字符编码
UNICODE
-双字节字符编码
UTF-8
-三字节字符编码
TCHAR operator[](int nIndex) const
;
用数组下标的方式来返回某个字符,
nIndex
是数组的下标,从
0
开始,注意
nIndex
的范围为
0
到
GetLength()-1
。注意你不能通过这种方式来改变该位置的字符。
void SetAt(int nIndex, TCHAR ch);
将
CSring
对象中
nIndex
位置的字符改为
ch
,注意
nIndex
的范围为
0
到
GetLength()-1
,注意改函数只能修改某个位置上的字符,不能增加字符。
operator LPCTSTR() const;
返回串数据的字符指针
串合并操作:
const CString& operator = (const CString& stringSrc);
throw(CMemoryException);
const CString& operator = (TCHAR ch);
throw(CMemoryException);
const CString& operator = (const unsigned char* psz);
throw(CMemoryException);
const CString& operator = (LPCWSTR lpsz);
throw(CMemoryException);
const CString& operator = (LPCSTR lpsz);
throw(CMemoryException);
赋值操作,不考虑对象的长度。
friend CString operator + (const CString& string1, const CString& string2);
throw(CMmeoryException);
friend CString operator + (const CString& string, TCHAR ch);
throw(CMmeoryException);
friend CString operator + (TCHAR ch, const CString& string);
throw(CMmeoryException);
friend CString operator + (const CString& string, LPCTSTR lpsz);
throw(CMmeoryException);
friend CString operator + (LPCTSTR lpsz, const CString& string);
throw(CMmeoryException);
const CString& operator += (const CString& string);
throw(CMemoryException);
const CString& operator += (TCHAR ch);
throw(CMemoryException);
const CString& operator += (LPCTSTR lpsz);
throw(CMemoryException);
比较操作符:只能使用两种
CString , LPCTSTR
BOOL operator ==(const CString& s1, const CString& s2);
BOOL operator ==(const CString& s1, LPCTSTR s2);
BOOL operator ==( LPCTSTR s1, const CString& s2);
BOOL operator !=(const CString& s1, const CString& s2);
BOOL operator !=(const CString& s1, LPCTSTR s2);
BOOL operator !=( LPCTSTR s1, const CString& s2);
BOOL operator <=(const CString& s1, const CString& s2);
BOOL operator <=(const CString& s1, LPCTSTR s2);
BOOL operator <=( LPCTSTR s1, const CString& s2);
BOOL operator >=(const CString& s1, const CString& s2);
BOOL operator >=(const CString& s1, LPCTSTR s2);
BOOL operator >=( LPCTSTR s1, const CString& s2);
BOOL operator <(const CString& s1, const CString& s2);
BOOL operator <(const CString& s1, LPCTSTR s2);
BOOL operator <( LPCTSTR s1, const CString& s2);
BOOL operator >(const CString& s1, const CString& s2);
BOOL operator >(const CString& s1, LPCTSTR s2);
BOOL operator >( LPCTSTR s1, const CString& s2);
int Compare(LPCTSTR lpsz) const;
区别大小写字幕
该函数对两个
CString
对象进行,如果内容完全一致则返回
0
;如果长度小于
lpsz
,则返回为
-1
,如果长度一致,但内容不同,则返回
-1
;如果长度大于
lpsz
,则返回
1
。
int CompareNoCase(LPCTSTR lpsz) const;
不区分大小写字母
该函数对两个
CString
对象进行,如果内容完全一致则返回
0
;如果长度小于
lpsz
,则返回为
-1
,如果长度一致,但内容不同,则返回
-1
;如果长度大于
lpsz
,则返回
1
。
int Compare(LPCTSTR lpsz) const;
区别大小写字幕
该函数对两个
CString
对象进行,如果内容完全一致则返回
0
;如果长度小于
lpsz
,则返回为
-1
,如果长度一致,但内容不同,则返回
-1
;如果长度大于
lpsz
,则返回
1
。
int CompareNoCase(LPCTSTR lpsz) const;
不区分大小写字母
该函数对两个
CString
对象进行,如果内容完全一致则返回
0
;如果长度小于
lpsz
,则返回为
-1
,如果长度一致,但内容不同,则返回
-1
;如果长度大于
lpsz
,则返回
1
。
int Collate(LPCTSTR lpsz) const;
区别大小写字幕
该函数对两个
CString
对象进行,如果内容完全一致则返回
0
;如果长度小于
lpsz
,则返回为
-1
,如果长度一致,但内容不同,则返回
-1
;如果长度大于
lpsz
,则返回
1
。
int CollateNoCase(LPCTSTR lpsz) const;
不区分大小写字母
该函数对两个
CString
对象进行,如果内容完全一致则返回
0
;如果长度小于
lpsz
,则返回为
-1
,如果长度一致,但内容不同,则返回
-1
;如果长度大于
lpsz
,则返回
1
。
Compare
和
Collate
函数的区别
:
Compare
函数和
==
运算符是等效的,
Compare
、
==
、
CompareNoCase
可识别
MBCS
和
Unicode
,
Collate
区别区域性,运算比
Compare
慢,仅当有必要遵守当前区域指定的排序规则时,才使用
Collate
函数。
抽取函数:
CString Mid(int nFirst) const;
throw(CMemoryException);
从一个
CString
对象中提取从
nFirst
开始到结尾的串,返回值是
CString
对象,注意返回值可能为空。
CString Mid(int nFirst , int nCount) const;
throw(CMemoryException);
从一个
CString
对象中提取从
nFirst
开始的长度为
nCount
的串,返回值是
CString
对象,注意返回值可能为空。
注意上述两种用法,当
CString
对象是表示多字节的字符串时,
nCount
表示的是一个
8
为字符的长度,例,对应
unicode
字符集要取
n
个字符则需要将
nCount=2*n
。
CString Left(int nCount) const;
throw(CMemoryException);
从一个
CString
对象中提取从开始位置,长度为
nCount
的串,返回值是
CString
对象,注意返回值可能为空。
CString Right(int nCount) const;
throw(CMemoryException);
从一个
CString
对象中提取从结束位置,长度为
nCount
的串,返回值是
CString
对象,注意返回值可能为空。
CString SpanIncluding(LPCTSTR lpszCharSet) const;
throw(CMemoryException);
从原
CString
对象中第一个字符开始提取字符
charactor
与指定的
LPCTSTR
传进行比对,如果该字符
charactor
在
LPCTSTR
串中,则继续进行下一个字符是否在
LPCTSTR
串中的比较,如果不在,则将该字符前的所有字符串返回构造成一个新的
CString
对象,如果从第一开始就不在
LPCTSTR
串中,则返回一个空
CString
对象。
CString SpanExcluding(LPCTSTR lpszCharSet) const;
throw(CMemoryException);
作用与
SpanIncluding
函数想反,表示从
CString
对象中一旦找到是
LPCTSTR
串中字符时就返回一个
CString
对象。
转换函数:
void MakeUpper();
将
CString
对象里的字符全部转换为大写字符。
void MakeLower();
将
CString
对象里的字符全部转换为小写字符。
void MakeReverse();
将
CString
对象里的字符串颠倒顺序。
int Replace(TCHAR chOld, TCHAR chNew);
int Replace(LPCTSTR lpszOld , LPCTSTR lpszNew);
该函数返回值表示被替换的
chOld
、
lpszOld
的个数。
int Remove(TCHAR ch);
从一个
CString
对象中删除
ch
字符串,返回值是删除的
ch
字符串个数,如果该对象没有改变,表示没有
ch
字符串被删除,返回值为
0
。
int Insert(int nIndex, TCHAR ch);
throw(CMemoryException);
int Insert(int nIndex, LPCTSTR pstr);
throw(CMemoryException);
向
CString
对象插入一个字符或者字符串,从
nIndex
位置开始开始插入,如果
nIndex
为
0
,表示从首部开始,如果
nIndex
大于
CString
对象的长度,则会从尾部开始。该函数返回改变长度后的对象的长度。
int Delete(int nIndex, int nCount = 1);
throw(CMemoryException);
对
CString
对象进行字符或者字符串进行删除操作,从
nIndex
位置开始的
nCount
个字符从
CString
对象中删除,返回值为操作后
CString
对象的长度。
void Format(LPCTSTR lpszFormat,…);
void Format(UINT nFormatID,…);
将
CString
对象值按照
lpszFormat
或者
nFormatID
定义的格式进行了修改。
CString str
=
"
大夫拉萨地方
";
int i=10;
str.Format("%i",i);
str
的最终值是
"10"
lpszFormat
详细内容参照:
http://msdn2.microsoft.com/en-us/library/hf4y5e3w(VS.80).aspx
里的说明。
printf Type Field Characters
Character
|
Type
|
Output format
|
c
|
int or wint_t
|
When used with printf functions,
指定为
8
位字符
; when used with wprintf functions,
指定位为
16
位字符。
.
|
C
|
int or wint_t
|
When used with printf functions,
指定位为
16
位字符
; when used with wprintf functions,
指定为
8
位字符
.
用法和
c
相反。
|
d
|
int
|
有符号的十进制整数
|
i
|
int
|
有符号的十进制整数
|
o
|
int
|
无符合的八进制整数
|
u
|
int
|
无符合的八进制整数
|
x
|
int
|
无符号的十六进制整数
, using "abcdef."
|
X
|
int
|
无符号的十六进制整数
, using "ABCDEF."
|
e
|
double
|
Signed value having the form [ – ]d.dddd e [sign]dd[d] where d is a single decimal digit, dddd is one or more decimal digits, dd[d] is two or three decimal digits depending on the output format and size of the exponent, and sign is + or –.
|
E
|
double
|
Identical to the e format except that E rather than e introduces the exponent.
|
f
|
double
|
Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision.
|
g
|
double
|
Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than –4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it.
|
G
|
double
|
Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate).
|
a
|
double
|
Signed hexadecimal double precision floating point value having the form [−]0xh.hhhh p±dd, where h.hhhh are the hex digits (using lower case letters) of the mantissa, and dd are one or more digits for the exponent. The precision specifies the number of digits after the point.
|
A
|
double
|
Signed hexadecimal double precision floating point value having the form [−]0Xh.hhhh P±dd, where h.hhhh are the hex digits (using capital letters) of the mantissa, and dd are one or more digits for the exponent. The precision specifies the number of digits after the point.
|
n
|
Pointer to integer
|
Number of characters successfully written so far to the stream or buffer; this value is stored in the integer whose address is given as the argument. See Security Note below.
|
p
|
Pointer to void
|
Prints the address of the argument in hexadecimal digits.
|
s
|
String
|
When used with printf functions, specifies a single-byte–character string; when used with wprintf functions, specifies a wide-character string. Characters are printed up to the first null character or until the precision value is reached.
|
S
|
String
|
When used with printf functions, specifies a wide-character string; when used with wprintf functions, specifies a single-byte–character string. Characters are printed up to the first null character or until the precision value is reached.
|
void FormatV(LPCTSTR lpszFormat,va_list argList);
将函数
Format
中关于
…
部分的内容用
va_list
变量来表示,功能和
Format
相同。
void TrimLeft();
void CString::TrimLeft(TCHAR chTarget);
void CString::TrimLeft(LPCTSTR lpszTargets);
当该函数没有参数时,对应一个
CString
对象,将去掉串的首部空白空格(新行、空格、
tab
字符)字符串。
当该函数有参数时,将从
CString
对象的开始去掉规定的字符
TCHAR
或字符串
lpszTargets
。
void TrimRight();
void CString::TrimRight(TCHAR chTarget);
void CString::TrimRight(LPCTSTR lpszTargets);
用法与
TrimLeft
相似,不过是处理
CString
对象的尾部字符。
void FormatMessage(LPCTSTR lpszFormat,…);
void FormatMessage(UINT nFormatID,…);
格式化消息字符串,
查找函数:
int Find(TCHAR ch) const;
int Find(LPCTSTR lpszSub) const;
int Find(TCHAR ch , int nStart) const;
int Find(LPCTSTR lpszSub , int nStart) const;
该函数用来在
CString
对象中查找字符
ch
或者字符串
lpszSub
,如果找到则返回第一个字符所在位置索引值。
nStart
表示该函数可以从规定的位置开始进行查找。
int ReverseFind(TCHAR ch) const;
在
CString
对象中找到最后一个字符
ch
,返回值是该位置值,如果没有找到返回
-1
。
int FindOneOf(LPCTSTR lpszCharSet) const;
从
CString
对象中查找到字符串
lpszCharSet
中任何一个字符就返回该位置值。
friend CArchive& operator <<(CArchive& ar, const CString& string);
throw(CMemoryException);
friend CArchive& operator >>(CArchive& ar, const CString& string);
throw(CMemoryException);
friend CDumpContext& operator <<(CDumpContext& dc, const CString& string);
throw(CMemoryException);
CArchiev<<CString
:将
CString
对象内容写入到
CArchive
中。
CArchiev>>CString
:将
CArchive
对象内容写入到
CString
中。
缓冲区访问函数:
LPTSTR GetBuffer(int nMinBufLength);
throw(CMemoryException);
返回
CString
对象的内容缓冲区的头指针,
nMinBufLength
是设定的内容缓冲区的大小,在实际的使用中采用
CString::GetBuffer(CString::GetLength())
或者用
CString::GetBuffer(0)
通过这个方法可以修改对象的内容。
void ReleaseBuffer(int nNewLength = -1);
按照
nNewLength
值的大小来最终确定
CString
对象的内容缓冲区的长度,更新
CString
对象内部的信息,这样才能保证下面的对象的其它操作。当使用
ReleaseBuffer()
或者
ReleaseBuffer(-1)
时,表示按照
CString
对象内容实际的大小来设定,会将
GetBuffer()
声明的多余的空间释放掉,如果
ReleaseBuffer(nNewlength >0)
时,则
CString
对象会按照
nNewLength
设定的值来调整
CString
内容。
LPTSTR GetBufferSetLength(int nNewLength);
throw(CMemoryException);
返回
CString
对象的内容缓冲区的头指针,
nNewLength
是设定的内容缓冲区的大小
|
GetBuffer(nMinBufLength)
|
GetBufferSetLength(nNewLength)
|
代码功能
|
CString str("ddddddddddddddd");
CString mm;
LPTSTR p;
int i;
p = str.GetBuffer(100);
strcpy(p,"hellohellohellohello");
str.ReleaseBuffer();
|
CString str("ddddddddddddddd");
CString mm;
LPTSTR p;
int i;
p = str. GetBufferSetLength(100);
strcpy(p,"hellohellohellohello");
str.ReleaseBuffer();
|
说明
|
调用GetBuffer(100)之前,str对象的长度是15
当调用GetBuffer(100)时,跟踪str对象的长度还是15
但是str的内容更新为” hellohellohellohello”(实际的长度为20)
调用ReleaseBuffer()后,str的长度为20
|
调用GetBuffer(100)之前,str对象的长度是15
当调用GetBufferSetLength(100)时,跟踪str对象的长度是100
但是str的内容更新为” hellohellohellohello”(实际的长度为20)
调用ReleaseBuffer()后,str的长度为20
|
void FreeExtra();
释放
CString
对象之前声明的不在使用的多余的内存空间,
LPTSTR LockBuffer();
LPTSTR UnLockBuffer();
这对函数表示对引用的内存块进行加锁解锁。调用
LockBuffer()
,它创建了一个字符串副本,并设置
CString
对象的引用计数器为
-1
,保护起内存区。当字符串内存区处于锁状态时,通过两种方法来保护串内容不被修改:
用于
Windows
的特殊函数:
BSTR AllocSysString() const;
throw(CMemoryException);
返回一个重新分配空间的串的指针。该函数将一个字符串转换为
unicode
字符集形式,使用完成后要调用
SysFreeString()
函数释放字符串。使用时如果你使用
MFC
共享动态库并在
debug
模式下创建应用程序则必须链接
MFC042D.DLL
,在申明的
stdafx.h
的头文件中包含
afxdisp.h
头文件。