- //A.构造函数,CStringT提供了19个构造函数,其中一个旨在.NET平台托管C++工程中才被编译,我只写几个我觉得常用的
- //1.默认构造函数,生成一个""字符串
- CString cstr1;
- //2.拷贝构造函数
- CString cstr2(cstr1);
- //3.从LPCSTR构造
- LPCSTR myCStr = "Hello CString";
- CString cstr3(myCStr);
- //4.从LPCWSTR构造
- LPCWSTR myCWStr = L"Hello CString";
- CString cstr4(myCWStr);
- //5.指定n个c字符构造
- char mycharacter = 'x';
- CString cstr5(mycharacter,10); //构造10个x的字符串
- //6.从指定n个w字符构造
- wchar_t mywcharacter = '测';
- CString cstr6(mywcharacter,5);
- //7.从unsigned char(也就是MBCS)构造
- unsigned char* pMbcs = NULL;
- CString cstr7(pMbcs);
- //8.从VARIANT构造
- BSTR bstr = SysAllocString(L"Hello World");
- VARIANT var;
- var.bstrVal = bstr; //var.bstrVal = L"Hello World"cstr8的字符信息不对。
- var.vt = VT_BSTR;
- SysFreeString(bstr);
- CString cstr8(var);
-
- //B.CString提供了8个赋值操作符,我只写我觉得几个常用的
- //1.CString到CString
- cstr8 = cstr7;
- //2.c char到CString
- cstr8 = mycharacter;
- //3.w char到CString
- cstr8 = mywcharacter;
- //4.uc char到CString
- cstr8 = pMbcs;
- //5.VARIANT到CString
- cstr8 = var;
-
- //C.字符串连接,我只写几个我觉得常用的
- //1.operator+ char
- cstr8 += mycharacter;
- //2.operator+ unsigned char
- cstr8 += unsigned char('c');
- //3.operator+ wchar_t
- cstr8 += mywcharacter;
- //4.operator+ VARIANT
- cstr8 += var;
- //CString + CString
- cstr8 += cstr7;
-
- //D.大小写转换
- cstr8.MakeUpper(); //转大写
- cstr8.MakeLower(); //转小写
-
- //E.CString定义了比较完整的比较操作,这有==、!=、<等等
-
- //F.操作
- //1.查找
- cstr8.Find('o'); //查找第一个'o'
- cstr8.Find('o',5); //从左起第5个查
- cstr8.FindOneOf(L"aeiou"); //查找集合中的任一个
- cstr8.ReverseFind('o'); //反向查找
- //2.裁剪
- cstr8.Trim(); //裁剪开始+末尾空格
- cstr8.Trim('o'); //裁剪左右'o'
- cstr8.Trim(L"aeiou"); //裁剪左右任何在集合中的字符
- cstr8.TrimLeft();
- cstr8.TrimLeft('o');
- cstr8.TrimLeft(L"aeiou");
- cstr8.TrimRight();
- cstr8.TrimRight('o');
- cstr8.TrimRight(L"aeiou");
- //3.取回目标字符串中被指定字符串包含的子串
- cstr8 = L"hello worldhello world";
- cstr7 = cstr8.SpanIncluding(L"mhellohaha");
- //4.取回原串,直到目标串中的任意一个字符出现
- cstr7 = cstr8.SpanExcluding(L"aiuw");
- //5.Inert
- cstr8.Insert(5,L"haha"); //指定位置插入字符串
- cstr8.Insert(6,'h'); //指定位置插入字符
- //6.Delete、Remove
- cstr8.Delete(5,2); //从指定位置(5)删除若干个(2)字符
- cstr8.Remove('o'); //移除所有'o'
- //7.Replace
- cstr8.Replace('o','a'); //把'o'替换为'a'
- cstr8.Replace(L"haha",L"toto"); //字符串替换
- //9.提取子串
- CString cstrRet;
- cstrRet = cstr8.Left(5); //提取最左n个字符
- cstrRet = cstr8.Mid(10); //从第n个字符到结尾
- cstrRet = cstr8.Mid(10,15); //提取指定区间字符串
- cstrRet = cstr8.Right(11); //最右边n个字符
- //10.反转
- cstr8.MakeReverse();
- //11.以特定分隔符提取
- int refn = 0;
- cstrRet = cstr8.Tokenize(L"aeiou",refn); //可以使用refn递归找到所有被分割的子串
- //12.载入dll或exe中的字符串资源
- cstr8.LoadString(100); //100为资源id,还有其他重载,略去
- //13.获取环境变量
- cstr8.GetEnvironmentVariable(L"windir");
- //14.格式化
- cstr8.Format(L"haha,now is%d with %s",100,L"old"); //format string 也可以是资源id
-
- //G.与BSTR的交互
- BSTR bstr1 = cstr8.AllocSysString();
- BSTR bstr2 = cstr8.SetSysString(&bstr1);
- SysFreeString(bstr2);
posted on 2013-03-18 21:58
聂文龙 阅读(1033)
评论(0) 编辑 收藏 引用 所属分类:
Visual C++