关于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