CString 成员函数用法大全.docx
《CString 成员函数用法大全.docx》由会员分享,可在线阅读,更多相关《CString 成员函数用法大全.docx(6页珍藏版)》请在冰豆网上搜索。
CString成员函数用法大全
CString的构造函数
CString();
例:
CStringcsStr;
CString(constCString&stringSrc);
例:
CStringcsStr("ABCDEF中文123456");
CStringcsStr2(csStr);
CString(TCHARch,intnRepeat=1);
例:
CStringcsStr('a',5);
//csStr="aaaaa"
CString(LPCTSTRlpch,intnLength);
例:
CStringcsStr("abcdef",3);
//csStr="abc"
CString(LPCWSTRlpsz);
例:
wchar_ts[]=L"abcdef";
CStringcsStr(s);
//csStr=L"abcdef"
CString(constunsignedchar*psz);
例:
constunsignedchars[]="abcdef";
constunsignedchar*sp=s;
CStringcsStr(sp);
//csStr="abcdef"
CString(LPCSTRlpsz);
例:
CStringcsStr("abcdef");
//csStr="abcdef"
intGetLength()const;
返回字符串的长度,不包含结尾的空字符。
例:
csStr="ABCDEF中文123456";
printf("%d",csStr.GetLength()); //16
voidMakeReverse();
颠倒字符串的顺序
例:
csStr="ABCDEF中文123456";
csStr.MakeReverse();
cout<voidMakeUpper();
将小写字母转换为大写字母
例:
csStr="abcdef中文123456";
csStr.MakeUpper();
cout<voidMakeLower();
将大写字母转换为小写字母
例:
csStr="ABCDEF中文123456";
csStr.MakeLower();
cout<intCompare(LPCTSTRlpsz)const;
区分大小写比较两个字符串,相等时返回0,大于时返回1,小于时返回-1
例:
csStr="abcdef中文123456";
csStr2="ABCDEF中文123456";
cout<intCompareNoCase(LPCTSTRlpsz)const;
不区分大小写比较两个字符串,相等时返回0,大于时返回1,小于时返回-1
例:
csStr="abcdef中文123456";
csStr2="ABCDEF中文123456";
cout<intDelete(intnIndex,intnCount=1)
删除字符,删除从下标nIndex开始的nCount个字符
例:
csStr="ABCDEF";
csStr.Delete(2,3);
cout<//当nIndex过大,超出对像所在内存区域时,函数没有任何操作。
//当nIndex为负数时,从第一个字符开始删除。
//当nCount过大,导致删除字符超出对像所在内存区域时,会发生无法预料的结果。
//当nCount为负数时,函数没有任何操作。
intInsert(intnIndex,TCHARch)
intInsert(intnIndex,LPCTSTRpstr)
在下标为nIndex的位置,插入字符或字符串。
返回插入后对象的长度
例:
csStr="abc";
csStr.Insert(2,'x');
cout< csStr="abc";
csStr.Insert(2,"xyz");
cout<//当nIndex为负数时,插入在对象开头
//当nIndex超出对象末尾时,插入在对象末尾
intRemove(TCHARch);
移除对象内的指定字符。
返回移除的数目
例:
csStr="aabbaacc";
csStr.Remove('a');
cout<intReplace(TCHARchOld,TCHARchNew);
intReplace(LPCTSTRlpszOld,LPCTSTRlpszNew);
替换字串
例:
csStr="abcdef";
csStr.Replace('a','x');
cout< csStr="abcdef";
csStr.Replace("abc","xyz");
cout<voidTrimLeft();
voidTrimLeft(TCHARchTarget);
voidTrimLeft(LPCTSTRlpszTargets);
从左删除字符,被删的字符与chTarget或lpszTargets匹配,一直删到第一个不匹配的字符为止
例:
csStr="aaabaacdef";
csStr.TrimLeft('a');
cout< csStr="aaabaacdef";
csStr.TrimLeft("ab");
cout<//无参数时删除空格
voidTrimRight();
voidTrimRight(TCHARchTarget);
voidTrimRight(LPCTSTRlpszTargets);
从右删除字符,被删的字符与chTarget或lpszTargets匹配,一直删到第一个不匹配的字符为止
例:
csStr="abcdeaafaaa";
csStr.TrimRight('a');
cout< csStr="abcdeaafaaa";
csStr.TrimRight("fa");
cout<//无参数时删除空格
voidEmpty();
清空
例:
csStr="abcdef";
csStr.Empty();
printf("%d",csStr.GetLength()); //0
BOOLIsEmpty()const;
测试对象是否为空,为空时返回零,不为空时返回非零
例:
csStr="abc";
cout< csStr.Empty();
cout<intFind(TCHARch)const;
intFind(LPCTSTRlpszSub)const;
intFind(TCHARch,intnStart)const;
intFind(LPCTSTRpstr,intnStart)const;
查找字串,nStart为开始查找的位置。
未找到匹配时返回-1,否则返回字串的开始位置
例:
csStr="abcdef";
cout< cout< cout< cout< cout< cout<//当nStart超出对象末尾时,返回-1。
//当nStart为负数时,返回-1。
intFindOneOf(LPCTSTRlpszCharSet)const;
查找lpszCharSet中任意一个字符在CString对象中的匹配位置。
未找到时返回-1,否则返回字串的开始位置
例:
csStr="abcdef";
cout<CStringSpanExcluding(LPCTSTRlpszCharSet)const;
返回对象中与lpszCharSet中任意匹配的第一个字符之前的子串
例:
csStr="abcdef";
cout<CStringSpanIncluding(LPCTSTRlpszCharSet)const;
从对象中查找与lpszCharSe中任意字符不匹配的字符,并返回第一个不匹配字符之前的字串
例:
csStr="abcdef";
cout<intReverseFind(TCHARch)const;
从后向前查找第一个匹配,找到时返回下标。
没找到时返回-1
例:
csStr="abba";
cout<voidFormat(LPCTSTRlpszFormat,...);
voidFormat(UINTnFormatID,...);
格式化对象,与C语言的sprintf函数用法相同
例:
csStr.Format("%d",13);
cout<TCHARGetAt(intnIndex)const;
返回下标为nIndex的字符,与字符串的[]用法相同
例:
csStr="abcdef";
cout<