上一节阐述了鸡啄米分割窗口的相关知识,本节开始介绍MFC的常用类,首先介绍CString类。
CString类简介
CString类作为MFC的常用类是正确的。 只要从事MFC开发,基本上可以说会遇到使用CString类的情况。 字符串使用比较普遍,CString类为字符串提供了方便的操作,给MFC开发人员带来了很高的开发效率,受到开发人员的欢迎。
大家使用VS2010的话,可能会遇到CStringT,但实际上是操作可变长度字符串的模板类。 CStringT模板类有三个实例: CString、CStringA和CStringW,它们分别提供对TCHAR、char和wchar_t字符串的操作。 char类型定义Ansi字符,wchar_t类型定义Unicode字符,TCHAR定义MFC工程属性对话框中的configuration properties-general-characters et属性如果此属性为usemulti-byti,则TCHAR类型定义Ansi字符;如果为Use Unicode Character Set,则定义Unicode字符。
三个字符串类的操作相同,但处理的字符类型不同。 鸡啄米以CString类为说明对象。
CString类的字符串操作
1. CString类的构造函数
虽然CString类有很多构造函数,但以下是一些常见的构造函数。
控制字符串(cstringstringsrc;
将已存在的CString对象stringSrc的内容复制到CString对象中。 例如:
c代码cstring str1(_ t (www.jizhuomi.com) ); //将常量字符串复制到str1cstringstr2(str1); 将str1的内容复制到str1字符串(lpctstrlpch,int nLength );
将字符串lpch中的第一个nLength字符复制到CString对象。 例如:
c代码cstringstr(_t(www.jizhuomi.com),3 ); //结构化字符串对象的内容为' www'cstring(tcharch,int nLength=1);
使用此函数构建的CString对象包含nLength个重复的ch字符。 例如:
c代码cstringstr(_t ) ' w ),3 ); //str是' www' 2. CString类的大小写转换及顺序转换函数
CString MakeLower (;
将字符串中的所有大小写转换为小写。
CString MakeUpper (;
将字符串中的所有小写字符转换为大写。
CString MakeReverse (;
反转字符串中所有字符的顺序。
例如:
c代码cstringstr(_t ) ' jizhuomi ' ); str.MakeLower (; //str是' jizhuomi'str.MakeUpper (; //str是' JIZHUOMI'str.MakeReverse (; //str是' IMOUHZIJ' 3. CString对象的连接
多个CString对象的连接可以通过重载运算符=实现。 例如:
c代码cstringstr(_t ) ' jizhuomi ' ); //str的内容为' jizhuomi'str=_t('www.' ) str _T ) '.'); //str为' www.jizhuomi.'str=_t(com ); //str为' www.jizhuomi.com '
4. CString对象的比较
CString对象的比较是==, 也可以使用Co实现重载运算符,如=、=、=和=
mpare和CompareNoCase成员函数实现。int Compare(PCXSTR psz) const;
将该CString对象与psz字符串比较,如果相等则返回0,如果小于psz则返回值小于0,如果大于psz则返回值大于0。
int CompareNoCase(PCXSTR psz) const throw();
此函数与Compare功能类似,只是不区分大小写。
例如:
C++代码 CString str1 = _T("JiZhuoMi"); CString str2 = _T("jizhuomi"); if (str1 == str2) { // 因为str1、str2不相等,所以不执行下面的代码 ... } if (0 == str1.CompareNoCase(str2)) { // 因为不区分大小写比较时,CompareNoCase函数返回0,所以执行下面的代码 ... }5. 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对象。
例如:
C++代码 CString str1 = _T("jizhuomi"); CString str2 = str1.Left(3); // str2为"jiz" str2 = str1.Right(2); // str2为"mi" str2 = str1.Mid(1,3); // str2为"izh" str2 = str1.Mid(5); // str2为"omi"6. 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。这里要注意,尽管是从后向前查找,但是位置的索引还是要从开始算起。
C++代码 CString str = _T("jizhuomi"); int nIndex1 = str.Find(_T("zh")); // nIndex1的值为2 int nIndex2 = str.FindOneOf(_T("mui")); // nIndex2的值为1 int nIndex3 = str.ReverseFind(_T('i')); // nIndex3的值为77. 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指定的字符,返回删除的字符个数。
例如:
C++代码 CString str = _T("jizhuomi"); int n1 = str.Replace(_T('i'), _T('j')); // str为"jjzhuomj",n1为2 int n2 = str.Delete(1,2); // str为"jhuomj",n2为6 int n3 = str.Remove(_T('j')); // str为"huom",n3为28. CString类的格式化字符串方法
使用CString类的Format成员函数可以将int、short、long、float、double等数据类型格式化为字符串对象。
void __cdecl Format(PCXSTR pszFormat,[, argument]...);
参数pszFormat为格式控制字符串;参数argument可选,为要格式化的数据,一般每个argument在pszFormat中都有对应的表示其类型的子字符串,int型的argument对应的应该是"%d",float型的应对应"%f",等等。
例如:
C++代码 CString str; int a = 1; float b = 2.3f; str.Format(_T("a=%d,b=%f"), a, b); // str为"a=1,b=2.300000"好了,关于CString类的内容就讲到这里了,用法不少,但仍不全,大家可以查看MSDN深入学习。鸡啄米谢谢大家耐心读完本节教程。
原文地址:http://www.jizhuomi.com/software/228.html
转载于:https://www.cnblogs.com/onedime/archive/2012/12/10/2811231.html