小石这此总结下CString的使用,在MFC编程中,CString作为我们常用的类。下面介绍下它:
CString类介绍
CString类作为MFC的常用类,当之无愧。可以这样说,只要是从事MFC开发,基本都会遇到使用CString类的场合。因为字符串的使用比较普遍,而CString类又提供了对字符串的便捷操作,所以它给MFC开发人员带来了高的开发效率,受到了开发者的欢迎。
CString类操作
一、构造函数
作为一个类,首先我们第一步肯定是要先去构造CString(const CString& stringSrc);,例如
CString str1(__T("www.daheng-image.com"));
将一个已经存在的CString对象stringSrc的内容拷贝到该CString对象。例如
CString str1(_T("www.daheng-image.com")); // 将常量字符串拷贝到str1
CString str2(str1); // 将str1的内容拷贝到str2
2 第二种构造函数CString(LPCTSTR lpch, int nLength),举个例子如下:
CString str(_T("www.daheng-image.com"),3); // 构造的字符串对象内容为"www"
3 第三种构造函数CString(TCHAR ch, int nLength = 1);
CString str(_T('w'),3); // str为"www"
二、CString类的大小写转换及顺序转换函数
CString& MakeLower();
将字符串中的所有大写字符转换为小写字符。
CString& MakeUpper();
将字符串中的所有小写字符转换为大写字符。
CString& MakeReverse();
将字符串中所有字符的顺序颠倒。
例如:
CString str(_T("shirunfa"));
str.MakeLower(); // str为"shirunfa"
str.MakeUpper(); // str为"SHIRUNFA"
str.MakeReverse(); // str为"AFNURIHS"
三、 CString对象的连接
多个CString对象的连接可以通过重载运算符+、+=实现。例如:
CString str(_T("daheng-image")); // str内容为"daheng-image"
str = _T("www.") + str + _T("."); // str为"www.daheng-image."
str += _T("com"); // str为"www.daheng-image.com"
str.append(__T(".cpm")) //str 为 daheng-image.com
四、 CString对象的比较
CString对象的比较可以通过==、!=、<、>、<=、>=等重载运算符实现,也可以使用Compare和CompareNoCase成员函数实现。
int Compare(PCXSTR psz) const;
将该CString对象与psz字符串比较,如果相等则返回0,如果小于psz则返回值小于0,如果大于psz则返回值大于0。
int CompareNoCase(PCXSTR psz) const throw();
此函数与Compare功能类似,只是不区分大小写。
例如:
CString str1 = _T("Shirunfa");
CString str2 = _T("shirunfa");
if (str1 == str2)
{
// 因为str1、str2不相等,所以不执行下面的代码
...
}
if (0 == str1.CompareNoCase(str2))
{
// 因为不区分大小写比较时,CompareNoCase函数返回0,所以执行下面的代码
...
}
五、. CString对象字符串的提取操作
CString Left(int nCount) const;
提取该字符串左边nCount个字符的子字符串,并返回一个包含这个子字符串的拷贝的CString对象。
CString Right(int nCount) const;
提取该字符串右边nCount个字符的子字符串,并返回一个包含这个子字符串的拷贝的CString对象。
CString Mid(int iFirst,int nCount) const;
提取该字符串中以索引iFirst位置开始的nCount个字符组成的子字符串,并返回一个包含这个子字符串的拷贝的CString对象。
CString Mid(int iFirst) const;
提取该字符串中以索引iFirst位置开始直至字符串结尾的子字符串,并返回一个包含这个子字符串的拷贝的CString对象。
例如:
CString str1 = _T("shirunfa");
CString str2 = str1.Left(3); // str2为"shi"
str2 = str1.Right(2); // str2为"fa"
str2 = str1.Mid(1,3); // str2为"hir"
str2 = str1.Mid(5); // str2为"nfa"
六、 CString对象字符串的查找操作
int Find(PCXSTR pszSub,int iStart=0) const throw( );
int Find(XCHAR ch,int iStart=0) const throw( );
在CString对象字符串的iStart索引位置开始,查找子字符串pszSub或字符ch第一次出现的位置,如果没有找到则返回-1。
int FindOneOf(PCXSTR pszCharSet) const throw( );
查找pszCharSet字符串中的任意字符,返回第一次出现的位置,找不到则返回-1。
int ReverseFind(XCHAR ch) const throw();
从字符串末尾开始查找指定的字符ch,返回其位置,找不到则返回-1。这里要注意,尽管是从后向前查找,但是位置的索引还是要从开始算起。
CString str = _T("shirunfa");
int nIndex1 = str.Find(_T("ru")); // nIndex1的值为2
int nIndex2 = str.FindOneOf(_T("hun")); // nIndex2的值为1
int nIndex3 = str.ReverseFind(_T('a')); // nIndex3的值为7
七 CString类对象字符串的替换与删除
int Replace(PCXSTR pszOld,PCXSTR pszNew);
用字符串pszNew替换CString对象中的子字符串pszOld,返回替换的字符个数。
int Replace(XCHAR chOld,XCHAR chNew);
用字符chNew替换CString对象中的字符chOld,返回替换的字符个数。
int Delete(int iIndex,int nCount = 1);
从字符串中删除iIndex位置开始的nCount个字符,返回删除操作后的字符串的长度。
int Remove(XCHAR chRemove);
删除字符串中的所有由chRemove指定的字符,返回删除的字符个数。
例如:
CString str = _T("shirunfa");
int n1 = str.Replace(_T('s'), _T('j')); // str为"jhirunfa",n1为1
int n2 = str.Delete(1,2); // str为"srunfa",n2为6
int n3 = str.Remove(_T('a')); // str为"shirunf",n3为1