关于CString的详细用法,参见:www.cnblogs.com/htj10/p/9341545.html

关于std::string的详细用法,参见:http://www.cplusplus.com/reference/string/string/?kw=string

1. 查找字符(串)-- 完全匹配,返回首次出现位置
CString:
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,否则返回字串的开始位置
std::string:

string (1)
size_t find (const string& str, size_t pos = 0) const;
c-string (2)
size_t find (const char* s, size_t pos = 0) const;
buffer (3)
size_t find (const char* s, size_t pos, size_t n) const;
character (4)
size_t find (char c, size_t pos = 0) const;

2. 查找字符串 -- 匹配字符串中某一个字符就行,返回首次出现位置
CString:
int FindOneOf( LPCTSTR lpszCharSet ) const;
查找lpszCharSet中任意一个字符在CString对象中的匹配位置。未找到时返回-1,否则返回字串的开始位置
std::string:

string (1)
size_t find_first_of (const string& str, size_t pos = 0) const;
c-string (2)
size_t find_first_of (const char* s, size_t pos = 0) const;
buffer (3)
size_t find_first_of (const char* s, size_t pos, size_t n) const;
character (4)
size_t find_first_of (char c, size_t pos = 0) const;

When pos is specified, the search only includes characters at or after position pos, ignoring any possible occurrences before pos.

3. 获取字符串的一部分
CString:
CString Left( int nCount ) const;    从左边取nCount个字符,当nCount=-1或0时,返回“”,如果 nCount 超过了字符串长度,则提取整个字符串。
CString Right( int nCount ) const;  从右边取nCount个字符
CString Mid( int nFirst ) const;       从nFirst位置到结尾 [nFirst, -1)
CString Mid( int nFirst, int nCount ) const;  从nFirst位置后nCount个字符 [nFirst, nFirst+nCount)
std::string:
string substr (size_t pos = 0, size_t len = npos) const;    从pos位置开始后len个字符,[pos,pos+len)  ,若没指定len则到字符串结尾 即 [pos,npos) 
The substring is the portion of the object that starts at character position pos and spans len characters (or until the end of the string, whichever comes first).

4. 字符串连接
std::string 和 CString 都有 + += 连接,当字符串想要保存二进制数据(其中会有\0字节),连接字符串最好要用 std::string,因为有时CString相加时遇见 \0 会截断。

std::string s("iid\0ss",6);//size=6
std::string s2("qq\0oo",4);//size=4
s = s + s2;//s.size()=10// s: iid\0ssqq\0o

转载于:https://www.cnblogs.com/htj10/p/11216277.html

std::string与MFC的CString的比较相关推荐

  1. wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString....转换

    wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString.... #include <strin ...

  2. CString、std::string格式化字符串

    2019独角兽企业重金招聘Python工程师标准>>> =============================CString=========================== ...

  3. wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString 以及system(command)...

    转载:http://blog.csdn.net/chocolateconanlan/article/details/4058868 wchar_t*,wchar_t,wchat_t数组,char,ch ...

  4. MFC C++ Cstring与string互转

    CString 转换成string 我试了很多的方法,都不行,我用的vs2010 ------解决方案-------------------- unicode: CString sz1 = L&quo ...

  5. MFC之CString与const char* string 转换

    MFC开发时经常会遇到CString.string/std::string.char*等类型相互转换的问题,现将一些思考与心得记下来与大家分享: 一.CString强制类型转换为const char* ...

  6. [转]wchar_t char std::string std::wstring CString 转换

    [转]wchar_t char std::string std::wstring CString 转换 1 #include <string> 2 // 使用CString必须使用MFC, ...

  7. C++中std::string与C-String字符数组的互相转换

    C语言中只有字符数组这一说法,没有C++专门封装的字符串类std::string.而字符数组C-String以\0作为结束符.std::string其实还是存储了C-String这个指针,只不过不同的 ...

  8. std::string格式化输入输出

    在C语言中: C函数有sprintf函数, 比较方便, 但是需要知道所需要的内存空间是多少. 在C++的框架MFC中: 在MFC中CString 有Format函数来格式化字符串. 很方便. 难过的是 ...

  9. 47.VC(custom)-string头文件与cstring头文件

    首先说string的头文件 1.<string> 包装了std的C++头文件 2.<string.h> 旧的C头文件 3.<cstring.h> 旧C头文件的std ...

最新文章

  1. TensorFlow2.0 系列开篇: Windows下GPU版本详细安装教程
  2. 无需卷积,完全基于Transformer的首个视频理解架构TimeSformer出炉
  3. js面试必考:this
  4. Hibernate 具体用法(自整理)
  5. Android配置----DDMS 连接真机(己ROOT),用file explore看不到data/data文件夹的解决办法...
  6. C++设计模式-Facade模式
  7. POJ C++程序设计 编程题#2 魔兽世界之二:装备
  8. Swift中的数据存储
  9. 小米 admob广告 ID_3月产品更新如何做好再营销广告归因!
  10. CDISC SDTM AE domain学习笔记 - 1
  11. 五、中枢背驰及盘整背驰
  12. 拿下阿里自动化测试岗23k*14薪offer的全程面试记录解析以及总结,一面二面三面,项目,功能,自动化,性能测试,面试题问答
  13. Flink部署——Debugging(开发实用,建议收藏)
  14. IM即时通信多房间聊天室仿微信聊天(一)
  15. Elasticsearch:深入理解 Dissect ingest processor
  16. 线性代数知识点总结——矩阵乘法、矩阵运算与性质、矩阵微积分
  17. word怎么取消目录
  18. 如何下载B站(bilibili)的视频
  19. Zigzag和蛇形矩阵
  20. 捕获SQLIntegrityConstraintViolationException

热门文章

  1. 澎思科技新出行人再识别(ReID)算法,刷新三大数据集最高记录
  2. 【Java】京东面试:说说MySQL的架构体系
  3. linux 开发板传文件,linux下使用minicom传送文件到开发板
  4. 无意中发现的一份清华大佬的代码模版,简洁易懂!
  5. 收藏 | 人脸检测之Retinaface
  6. CVPR 2021 | 姿态可控的语音驱动
  7. include引入php报错,如何解决引入php文件报错的问题
  8. 从零开始编写深度学习库(二)FullyconnecteLayer CPU编写
  9. rpm打包的流程包括以下哪些_Linux 下RPM打包制作流程
  10. IDEA UML图 实战