您的位置:首页 > 其它

MFC CString的用法

2014-04-23 17:53 267 查看
编译器:VS2012

1.CString位于头文件afx.h中.

1) CString类的构造函数

CString类有很多构造函数,这里只介绍几个比较常用的:

CString(const CString& stringSrc);

将一个已经存在的CString对象stringSrc的内容拷贝到该CString对象。例如:

CString str1(_T(jizhuomi)); // 将常量字符串拷贝到str1

CString str2(str1); // 将str1的内容拷贝到str2

CString(LPCTSTR lpch,int nLength);

将字符串lpch中的前nLength个字符拷贝到该CString对象。例如:

CString str(_T("wwwjizhuomi"),3); // 构造的字符串对象内容为"www"

CString(TCHAR ch,int nLength = 1);

使用此函数构造的CString对象中将含有nLength个重复的ch字符。例如:

CString str(_T('w'),3); // str为"www"

2)CString对象的连接

多个CString对象的连接可以通过重载运算符+、+=实现。例如:

CString str(_T("jizhuomi")); // str内容为"jizhuomi"

str = _T("www") + str + _T("-"); // str为"wwwjizhuomi-"

str += _T("com"); // str为wwwjizhuomi-com

2.数字转换到CString类型

int a,b;
double c,d;
a=11,b=12;
c=3.11,d=4.22;
int isum=a+b;
double fsum=c+d;
CString str1,str2,str;
str1.Format(_T("%d+%d=%d\n"),a,b,isum);//int转换为cstring
str2.Format(_T("%f+%f=%f"),c,d,fsum);//double转换为cstring
str=str+str1;
//cstring累加
str=str+str2;
MessageBox(str);//检验测试结果

CString转换为数字型int

CString num1("12");
CString num2("55");
int a,b;
a=_ttoi(num1);
b=_ttoi(num2);
int sum=a+b;
CString str;
str.Format(_T("%d+%d=%d"),a,b,sum);

MessageBox(str);

3.字符串与CString类型

char * p = "This is a test";

或者象下面这样更具有 Unicode 意识:

TCHAR * p = _T("This is a test")



LPTSTR p = _T("This is a test");

你可以使用下面任意一种写法:

CString s = "This is a test"; // 8-bit only

CString s = _T("This is a test"); // Unicode-aware

CString s("This is a test"); // 8-bit only

CString s(_T("This is a test")); // Unicode-aware

CString s = p;

CString s(p);

用这些方法可以轻松将常量字符串或指针转换成 CString。
CString 转化成 char* 之一:强制类型转换 为
LPCTSTR;

也可以CString graycat("GrayCat");

CString s;

s.Format("Mew! I love %s",graycat);
4.CString 的一些常用的函数:

int
GetLength( ) const;

返回字符串的长度,不包含结尾的空字符。
例:csStr="ABCDEF中文123456";
    printf("%d",csStr.GetLength());       //16

void MakeReverse( );
颠倒字符串的顺序
例:csStr="ABCDEF中文123456";
    csStr.MakeReverse();
    cout<<csStr;                  //654321文中FEDCBA

void MakeUpper(
);

将小写字母转换为大写字母
例:csStr="abcdef中文123456";
    csStr.MakeUpper();
    cout<<csStr;                  //ABCDEF中文123456

void MakeLower( );
将大写字母转换为小写字母
例:csStr="ABCDEF中文123456";
    csStr.MakeLower();
    cout<<csStr;                  //abcdef中文123456

int Compare( LPCTSTR lpsz ) const;
区分大小写比较两个字符串,相等时返回0,大于时返回1,小于时返回-1
例:csStr="abcdef中文123456";
    csStr2="ABCDEF中文123456";
    cout<<csStr.CompareNoCase(csStr2);             //0

int CompareNoCase( LPCTSTR lpsz ) const;
不区分大小写比较两个字符串,相等时返回0,大于时返回1,小于时返回-1
例:csStr="abcdef中文123456";
    csStr2="ABCDEF中文123456";
    cout<<csStr.CompareNoCase(csStr2);             //-1

int Delete( int nIndex, int nCount = 1
)

删除字符,删除从下标nIndex开始的nCount个字符
例:csStr="ABCDEF";
    csStr.Delete(2,3);
    cout<<csStr;              //
ABF

//当nIndex过大,超出对像所在内存区域时,函数没有任何操作。

//当nIndex为负数时,从第一个字符开始删除。

//当nCount过大,导致删除字符超出对像所在内存区域时,会发生无法预料的结果。

//当nCount为负数时,函数没有任何操作。

int Insert( int nIndex, TCHAR ch )

int Insert( int nIndex, LPCTSTR pstr )

在下标为nIndex的位置,插入字符或字符串。返回插入后对象的长度
例:csStr="abc";
    csStr.Insert(2,'x');
    cout<<csStr;                     //abxc
    csStr="abc";
    csStr.Insert(2,"xyz");
    cout<<csStr;                     //abxyzc
//当nIndex为负数时,插入在对象开头

//当nIndex超出对象末尾时,插入在对象末尾

int Remove( TCHAR ch );
移除对象内的指定字符。返回移除的数目
例:csStr="aabbaacc";
    csStr.Remove('a');
    cout<<csStr;                     //bbcc

int Replace( TCHAR chOld, TCHAR chNew );

int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );

替换字串
例:csStr="abcdef";
    csStr.Replace('a','x');
    cout<<csStr;                    //xbcdef
    csStr="abcdef";
    csStr.Replace("abc","xyz");
    cout<<csStr;                    //xyzdef

void TrimLeft( );

void TrimLeft( TCHAR chTarget );

void TrimLeft( LPCTSTR lpszTargets );

从左删除字符,被删的字符与chTarget或lpszTargets匹配,一直删到第一个不匹配的字符为止
例:csStr="aaabaacdef";
    csStr.TrimLeft('a');
    cout<<csStr;                //baacdef
    csStr="aaabaacdef";
    csStr.TrimLeft("ab");
    cout<<csStr;                //cdef
//无参数时删除空格

void TrimRight( );

void TrimRight( TCHAR chTarget );

void TrimRight( LPCTSTR lpszTargets );

从右删除字符,被删的字符与chTarget或lpszTargets匹配,一直删到第一个不匹配的字符为止
例:csStr="abcdeaafaaa";
    csStr.TrimRight('a');
    cout<<csStr;               //abcdeaaf
    csStr="abcdeaafaaa";
    csStr.TrimRight("fa");
    cout<<csStr;                //abcde

//无参数时删除空格

void Empty( );
清空
例:csStr="abcdef";
    csStr.Empty();
    printf("%d",csStr.GetLength());    //0

BOOL IsEmpty( ) const;
测试对象是否为空,为空时返回零,不为空时返回非零
例:csStr="abc";
    cout<<csStr.IsEmpty();         //0;
    csStr.Empty();
    cout<<csStr.IsEmpty();         //1;

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,否则返回字串的开始位置
例:csStr="abcdef";
    cout<<csStr.Find('b');       //1
    cout<<csStr.Find("de");      //3
    cout<<csStr.Find('b',3);     //-1
    cout<<csStr.Find('b',0);     //1
    cout<<csStr.Find("de",4);    //-1
    cout<<csStr.Find("de",0);    //3
//当nStart超出对象末尾时,返回-1。

//当nStart为负数时,返回-1。

int FindOneOf( LPCTSTR lpszCharSet ) const;
查找lpszCharSet中任意一个字符在CString对象中的匹配位置。未找到时返回-1,否则返回字串的开始位置
例:csStr="abcdef";
    cout<<csStr.FindOneOf("cxy");       //2

CString SpanExcluding( LPCTSTR lpszCharSet
) const;

返回对象中与lpszCharSet中任意匹配的第一个字符之前的子串
例:csStr="abcdef";
    cout<<csStr.SpanExcluding("cf");    //ab

CString SpanIncluding( LPCTSTR lpszCharSet
) const;

从对象中查找与lpszCharSe中任意字符不匹配的字符,并返回第一个不匹配字符之前的字串
例:csStr="abcdef";
    cout<<csStr.SpanIncluding("fdcba");    //abcd

int ReverseFind( TCHAR ch ) const;
从后向前查找第一个匹配,找到时返回下标。没找到时返回-1
例:csStr="abba";
    cout<<csStr.ReverseFind('a');        //3

void Format( LPCTSTR lpszFormat, ... );

void Format( UINT nFormatID, ... );

格式化对象,与C语言的sprintf函数用法相同
例:csStr.Format("%d",13);
    cout<<csStr;                       //13

TCHAR GetAt( int nIndex ) const;
返回下标为nIndex的字符,与字符串的[]用法相同
例:csStr="abcdef";
    cout<<csStr.GetAt(2);             //c

//当nIndex为负数或超出对象末尾时,会发生无法预料的结果。

void SetAt( int nIndex, TCHAR ch );
给下标为nIndex的字符重新赋值
例:csStr="abcdef";
    csStr.SetAt(2,'x');
    cout<<csStr;                      //abxdef

//当nIndex为负数或超出对象末尾时,会发生无法预料的结果。

CString Left( int nCount ) const;
从左取字串
例:csStr="abcdef";
    cout<<csStr.Left(3);           //abc

//当nCount等于0时,返回空。

//当nCount为负数时,返回空。

//当nCount大于对象长度时,返回值与对象相同。

CString Right( int nCount ) const;
从右取字串
例:csStr="abcdef";
    cout<<csStr.Right(3);           //def

//当nCount等于0时,返回空。

//当nCount为负数时,返回空。

//当nCount大于对象长度时,返回值与对象相同。

CString Mid( int nFirst ) const;

CString Mid( int nFirst, int nCount ) const;

从中间开始取字串
例:csStr="abcdef";
    cout<<csStr.Mid(2);           //cdef
    csStr="abcdef";
    cout<<csStr.Mid(2,3);         //cde

//当nFirst为0和为负数时,从第一个字符开始取。

//当nFirst等于对象末尾时,返回空字串。

//当nFirst超出对象末尾时,会发生无法预料的结果。

//当nCount超出对象末尾时,返回从nFirst开始一直到对象末尾的字串

//当nCount为0和为负数时,返回空字串。

LPTSTR GetBuffer( int nMinBufLength );
申请新的空间,并返回指针
例:csStr="abcde";
    LPTSTR pStr=csStr.GetBuffer(10);
    strcpy(pStr,"12345");
    csStr.ReleaseBuffer();
    pStr=NULL;
    cout<<csStr                 //12345

//使用完GetBuffer后,必须使用ReleaseBuffer以更新对象内部数据,否则会发生无法预料的结果。

void ReleaseBuffer( int nNewLength = -1
);

使用GetBuffer后,必须使用ReleaseBuffer以更新对象内部数据
例:csStr="abc";
    LPTSTR pStr=csStr.GetBuffer(10);
    strcpy(pStr,"12345");
    cout<<csStr.GetLength();       //3(错误的用法)
    csStr.ReleaseBuffer();
    cout<<csStr.GetLength();       //5(正确)
    pStr=NULL;
//CString对象的任何方法都应在ReleaseBuffer之后调用

LPTSTR GetBufferSetLength( int nNewLength
);

申请新的空间,并返回指针
例:csStr="abc";
    csStr.GetBufferSetLength(20);
    cout<<csStr;                  //abc
    count<<csStr.GetLength();     //20;
    csStr.ReleaseBuffer();
    count<<csStr.GetLength();     //3;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  MFC