VC编程资料总结_vc编程总结

2020-02-27 其他工作总结 下载本文

VC编程资料总结由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“vc编程总结”。

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

● 转换大小写:MakeUpper、MakeLower ● 转换顺序: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){

MeageBox(_T(“str1 等于 str2”));}else{

MeageBox(_T(“str1 不等于 str2”));}

③、字符串的查找:

Find、ReverseFind、FindOneOf 三个函数可以实现字符串的查找操作

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

⑤、字符串的提取:

Left、Mid、Right 三个函数分别实现从 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

⑥、单个字符的修改: GetAt、SetAt 可以获取与修改 CString 对象中的单个 TCHAR 类型字符;

操作符也可以获取 CString 对象中的单个字符,但为只读的,不能进行修改;

CString str(_T(“abcd”));

str.SetAt(0, 'k');//str == kbck TCHAR ch = str.GetAt(2);//ch == c

⑦、其他类型与 CString 对象类型的转换:

● 格式化字符串:Format 方法,实现从 int、long 等数值类型、TCHAR、TCHAR * 等类型向 CString 类型的转换;

int num = 6;CString str;

str.Format(_T(“%d”), num);

● CString 类型向 int 等数值类型、TCHAR * 类型的转换: TCHAR *pszBuf = str.GetBuffer();str.ReleaseBuffer();

TCHAR *p =(LPTSTR)(LPCTSTR)str;

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(CString)、sizeof(str)

正确的求法:str.GetLength()*sizeof(TCHAR)

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

转载]CString、TCHAR*、char*转换

[转载]CString、TCHAR*、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((LPSTR)chRtn,OLE2A(str.LockBuffer()));return chRtn;} //参考

/////////////////////// //Pocket PC上的UNICODE和ANSI字符串

//By Vaili Philippov, September 26, 2001.//杨方思歧 译

//////////////////////// /* *********************************************************************** * 函 数 名:GetAnsiString * 描 述:将CString(unicode)转换为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;}

////////////////////////////////////////////////////////////////////////////////////////////// WideCharToMultiByte和MultiByteToWideChar函数的用法

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

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

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

常用的代码页由CP_ACP和CP_UTF8两个。

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

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

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,(LPWSTR)pUnicode, 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,(LPWSTR)pUnicode, 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;}

《VC编程资料总结.docx》
将本文的Word文档下载,方便收藏和打印
推荐度:
VC编程资料总结
点击下载文档
相关专题 vc编程总结 资料 VC vc编程总结 资料 VC
[其他工作总结]相关推荐
    [其他工作总结]热门文章
      下载全文