聪明文档网

聪明文档网

最新最全的文档下载
当前位置: 首页> VC编程资料总结

VC编程资料总结

时间:2014-07-24    下载该word文档
CString类的完美总结
2012-05-04 15:50:50 我来说两句
收藏 我要投稿
①、CString 类对象的初始化: CString str;
CString str1(_T("abc"; CString str2 = _T("defg";
TCHAR szBuf[] = _T("kkk"; CString str3(szBuf; CString str4 = szBuf;
TCHAR *p = _T("1k2"; //TCHAR * 转换为 CString CString str5(p; CString str6 = p;
CString str7(str1; CString str8 = str7;
②、字符串基本操作: 长度:GetLength(; CString str(_T("abc";
int len = str.GetLength(; //len == 3
是否为空,即不含字符:IsEmpty(;

清空字符串:Empty(; CString str(_T("abc";
BOOL mEmpty = str.IsEmpty(; //mEmpty == FALSE str.Empty(;
mEmpty = str.IsEmpty(; //mEmpty == TRUE
转换大小写:MakeUpperMakeLower 转换顺序:MakeReverse CString str(_T("Abc"; str.MakeUpper(; //str == ABC str.MakeLower(; //str == abc str.MakeReverse(; //str == cba
字符串的连接:++= CString str(_T("abc";
str = _T("de" + str + _T("kp"; //str == deabckp str += _T("123"; //str == deabckp123 TCHAR szBuf[] = _T("789";
str += szBuf; //str == deabckp123789
字符串的比较:==!=(<><=>= 不常用Compare(区分大小写CompareNoCase(不区分大小写
CString str1(_T("abc"; CString str2 = _T("aBc"; if (str1 == str2{
MessageBox(_T("str1 等于 str2";

}else{
MessageBox(_T("str1 不等于 str2"; }
③、字符串的查找:
FindReverseFindFindOneOf 三个函数可以实现字符串的查找操
Find 从指定位置开始查找指定的字符或者字符串,返回其位置,找不到返回 -1
举例:
CString str(_T("abcdefg";
int idx = str.Find(_T("cde", 0; //idx 的值为2;
ReverseFind 从字符串末尾开始查找指定的字符,返回其位置,找不到返回 -1,虽然是从后向前查找,但是位置为从开始算起;
CString str(_T("abcdefg";
int idx = str.ReverseFind('e'; //idx 的值为4;
FindOneOf 查找参数中给定字符串中的任意字符,返回第一次出现的位置,找不到返回 -1
CString str(_T("abcabcd";
int idx = str.FindOneOf(_T("cbd"; //idx 的值为1;
④、字符串的替换与删除:
Replace 替换 CString 对象中的指定的字符或者字符串,返回替换的个数,无匹配字符返回 0
CString str(_T("abcdabc";

int num = str.Replace('b', 'k'; //str == akcdakc, num == 2
CString str(_T("abcdabc";
int num = str.Replace(_T("bc", _T("kw"; //str == akwdakw, num == 2
Remove 删除 CString 对象中的指定字符,返回删除字符的个数,多个时都会删除;
CString str(_T("abcdabcb";
int num = str.Remove('b'; //str == acdac, num == 3
Delete 删除 CString 对象中的指定位置的字符,返回处理后的字符串长度;
CString str(_T("abcd";
int num = str.Delete(1, 3; //str == a, num == 1
⑤、字符串的提取:
LeftMidRight 三个函数分别实现从 CString 对象的 左、中、 进行字符串的提取操作;
CString str(_T("abcd";
CString strResult = str.Left(2; //strResult == ab strResult = str.Mid(1; //strResult == bcd strResult = str.Mid(0, 2; //strResult == ab strResult = str.Right(2; //strResult == cd
⑥、单个字符的修改:

GetAtSetAt 可以获取与修改 CString 对象中的单个 TCHAR 类型字符;
操作符也可以获取 CString 对象中的单个字符,但为只读的,不能进行修改;
CString str(_T("abcd";
str.SetAt(0, 'k'; //str == kbck TCHAR ch = str.GetAt(2; //ch == c
⑦、其他类型与 CString 对象类型的转换:
格式化字符串:Format 方法,实现从 intlong 等数值类型、TCHARTCHAR * 等类型向 CString 类型的转换;
int num = 6; CString str;
str.Format(_T("%d", num;
CString 类型向 int 等数值类型、TCHAR * 类型的转换: TCHAR *pszBuf = str.GetBuffer(; str.ReleaseBuffer(;
TCHAR *p = (LPTSTR(LPCTSTRstr;
CString str1(_T("123"; int num = _ttoi(str1;
⑧、CString 对象的 Ansi Unicode 转换:

大家可以直接使用上节课给大家讲解的方法,此外这里给大家介绍一种从 Ansi 转换到 Unicode 的隐含方法:
//当前工程环境为Unicode CString str; str = "abc"; char *p = "defg"; str = p;
⑨、 CString 对象字符串所占用的字节数: CString str = _T("abc";
错误的求法:sizeof(CStringsizeof(str 正确的求法:str.GetLength(*sizeof(TCHAR

⑩、当作为 TCHAR * 类型传参时,确保申请了足够用的空间,比如使用 GetModuleFileName 函数


转载]CStringTCHAR*char*转换
[转载]CStringTCHAR*char*转换 char*TCHAR*转换CString CString str(**** 下面详细写一下其它转换 ////////////////////////////// /* *********************************************************************** * 函数: TransCStringToTCHAR * 描述:将CString 转换为 TCHAR* * 日期:
***********************************************************************
*/ TCHAR* CPublic::CString2TCHAR(CString &str { int iLen = str.GetLength(; TCHAR* szRs = new TCHAR[iLen]; lstrcpy(szRs, str.GetBuffer(iLen; str.ReleaseBuffer(; return szRs; }
/* *********************************************************************** * 函数: TCHAR2Char * 描述:将TCHAR* 转换为 char* * 日期: *********************************************************************** */ char* TCHAR2char(TCHAR* tchStr { int iLen = 2*wcslen(tchStr;//CString,TCHAR汉字算一个字符,因此不用普通计算长度 char* chRtn = new char[iLen+1] wcstombs(chRtn,tchStr,iLen+1;//转换成功返回为非负值 return chRtn; }
/* *********************************************************************** * 函数: char2tchar * 描述:将 char* 转换为 TCHAR* * 日期:
*********************************************************************** */ TCHAR *char2tchar(char *str { int iLen = strlen(str;
TCHAR *chRtn = new TCHAR[iLen+1];
mbstowcs(chRtn, str, iLen+1; return chRtn;
}
/* *********************************************************************** * 函数: CString2char * 描述:将CString转换为 char* * 日期:
*********************************************************************** */ char* CPublic::CString2char(CString &str { int len = str.GetLength(; char* chRtn = (char*malloc((len*2+1*sizeof(char;//CString的长度中汉字算一个长度 memset(chRtn, 0, 2*len+1; USES_CONVERSION; strcpy((LPSTRchRtn,OLE2A(str.LockBuffer(; return chRtn; } //参考 /////////////////////// //Pocket PC上的UNICODEANSI字符串
//By Vassili Philippov, September 26, 2001.
//杨方思歧 //////////////////////// /* *********************************************************************** * 名:GetAnsiString * 述:将CStringunicode)转换为char*(ANSI * 数:CString &s 要转换的CString * 值:返回转换结果 * 创建日期: * 最后修改:
*********************************************************************** */ char* GetAnsiString(const CString &s {
int nSize = 2*s.GetLength(; char *pAnsiString = new char[nSize+1]; wcstombs(pAnsiString, s, nSize+1; return pAnsiString; }
////////////////////////////////////////////////////////////////////////////////////////////// WideCharToMultiByteMultiByteToWideChar函数的用法

支持Unicode编码,需要多字节与宽字节之间的相互转换

WideCharToMultiByte的代码页用来标记与新转换的字符串相关的代码页。

MultiByteToWideChar的代码页用来标记与一个多字节字符串相关的代码页。

常用的代码页由CP_ACPCP_UTF8两个。

使用CP_ACP代码页就实现了ANSIUnicode之间的转换。

使用CP_UTF8代码页就实现了UTF-8Unicode之间的转换。

wstring AnsiToUnicode(( const string& str { int len = 0;
len = str.length(;
int unicodeLen = ::MultiByteToWideChar( CP_ACP, 0, str.c_str(,-1,NULL,0 ;
wchar_t * pUnicode;
pUnicode = new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1*sizeof(wchar_t;
::MultiByteToWideChar( CP_ACP,0, str.c_str(,-1, (LPWSTRpUnicode, unicodeLen ;
wstring rt;
rt = ( wchar_t* pUnicode;
delete pUnicode; return rt; }
string UnicodeToAnsi( const wstring& str {
char* pElementText; int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_ACP, 0, str.c_str(, -1, NULL, 0, NULL, NULL ;
pElementText = new char[iTextLen + 1];
memset( ( void* pElementText, 0, sizeof( char * ( iTextLen + 1 ;
::WideCharToMultiByte( CP_ACP, 0, str.c_str(, -1, pElementText,iTextLen,NULL,NULL ;
string strText;
strText = pElementText;
delete[] pElementText;

return strText; }
wstring UTF8ToUnicode(( const string& str { int len = 0;
len = str.length(;
int unicodeLen = ::MultiByteToWideChar( CP_UTF8, 0, str.c_str(,-1,NULL,0 ;
wchar_t * pUnicode;

pUnicode = new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1*sizeof(wchar_t;
::MultiByteToWideChar( CP_UTF8,0, str.c_str(,-1, (LPWSTRpUnicode, unicodeLen ;
wstring rt; rt = ( wchar_t* pUnicode; delete pUnicode; return rt; }
string UnicodeToUTF8( const wstring& str {
char* pElementText; int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_UTF8, 0, str.c_str(, -1, NULL, 0, NULL, NULL ;
pElementText = new char[iTextLen + 1];
memset( ( void* pElementText, 0, sizeof( char * ( iTextLen + 1 ;
::WideCharToMultiByte( CP_UTF8, 0, str.c_str(, -1, pElementText,iTextLen,NULL,NULL ;
string strText;
strText = pElementText;
delete[] pElementText;

return strText;
}


免费下载 Word文档免费下载: VC编程资料总结

  • 29.8

    ¥45 每天只需1.0元
    1个月 推荐
  • 9.9

    ¥15
    1天
  • 59.8

    ¥90
    3个月

选择支付方式

  • 微信付款
郑重提醒:支付后,系统自动为您完成注册

请使用微信扫码支付(元)

订单号:
支付后,系统自动为您完成注册
遇到问题请联系 在线客服

常用手机号:
用于找回密码
图片验证码:
看不清?点击更换
短信验证码:
新密码:
 
绑定后可用手机号登录
请不要关闭本页面,支付完成后请点击【支付完成】按钮
遇到问题请联系 在线客服