本文主要介绍C++中的string类的常见用法。
string是C++标准库的一个重要的部分,主要用于字符串处理。可以使用输入输出流方式直接进行string操作,也可以通过文件等手段进行string操作。同时,C++的算法库对string类也有着很好的支持,并且string类还和c语言的字符串之间有着良好的接口。
方法一:使用 c_str() 方法,代码(stringsimple.cpp)如下:
- #include <string>
- #include <iostream>
- #include <stdio.h>
- using namespace std;
- int main()
- {
- string strOutput = "Hello World";
- cout << "[cout] strOutput is: " << strOutput << endl;
- // string 转换为 char*
- const char* pszOutput = strOutput.c_str();
- printf("[printf] strOutput is: %s\n", pszOutput);
- return 0;
- }
编译并执行上述代码,结果如下:
上述代码执行结果说明:
data()方法与c_str()方法相似,都返回 const char* 类型。两者区别和联系如下:
示例代码如下:
- #include <string>
- #include <iostream>
- #define HELLOSTR "Hello World"
- using namespace std;
- int main()
- {
- string strOutput = "Hello World";
- int nLen = strOutput.length();
- cout << "the length of strOutput is: " << nLen << endl;
- if (0 == strOutput.compare(HELLOSTR))
- {
- cout << "strOutput equal with macro HELLOSTR" << endl;
- }
- return 0;
- }
编译并执行上述代码,结果如下:
[root@node1 /opt/liitdar/mydemos/simples]# ./stringsimple2 the length of strOutput is: 11strOutput equal with macro HELLOSTR[root@node1 /opt/liitdar/mydemos/simples]#
上述代码执行结果说明:
可使用 empty() 方法对string类型的对象进行判空,如下:
- if (str2.empty())
- {
- cout << "str2 is empty." << endl;
- }
将 char*、char[] 转换为 string 类型时,直接进行赋值操作,将 char*、char[] 的变量赋值给 string 对象即可。
说明:这里所说的“赋值”操作,实际上是将 char*、char[] 定义的字符串的首地址赋值给 string 对象了。
示例代码(stringtochar.cpp)如下:
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- const char* pszName = "liitdar";
- char pszCamp[] = "alliance";
- string strName;
- string strCamp;
- strName = pszName;
- strCamp = pszCamp;
- cout << "strName is: " << strName << endl;
- cout << "strCamp is: " << strCamp << endl;
- return 0;
- }
编译并执行上述代码,结果如下:
使用string类的find方法,在字符串中检索自字符串是否存在。
用法如下:
- size_t find (const string& str, size_t pos = 0) const;
- size_t find (const char* s, size_t pos = 0) const;
- size_t find (const char* s, size_t pos, size_t n) const;
- size_t find (char c, size_t pos = 0) const;
find函数返回值:
The position of the first character of the first match. If no matches were found, the function returns string::npos.
size_t is an unsigned integral type (the same as member type string::size_type).
find方法的示例代码(string_find_test1.cpp)如下:
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- // 待检索的字符串
- string strOutput = "|0|1|2|";
- // 需要检索的子串
- string strObj = "|1|";
- // 子串位于字符串中的位置
- size_t nLoc = strOutput.find(strObj);
- // 如果检索到子串在字符串中,则打印子串的位置
- if (nLoc != string::npos)
- {
- cout << "nLoc is: " << nLoc << endl;
- }
- return 0;
- }
编译并执行上述代码,结果如下:
使用string类的insert方法,向字符串中插入字符(串)。官方的定义如下:
Inserts additional characters into the string right before the character indicated by pos (or p).
string (1) | string& insert (size_t pos, const string& str); |
substring (2) | string& insert (size_t pos, const string& str, size_t subpos, size_t sublen); |
c-string (3) | string& insert (size_t pos, const char* s); |
buffer (4) | string& insert (size_t pos, const char* s, size_t n); |
fill (5) | string& insert (size_t pos, size_t n, char c); void insert (iterator p, size_t n, char c); |
single character (6) | iterator insert (iterator p, char c); |
range (7) | template <class InputIterator> void insert (iterator p, InputIterator first, InputIterator last); |
insert方法的示例代码(string_insert_test1.cpp)如下:
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- string strDemo = "I am";
- strDemo.insert(4, " good.");
- cout << "strDemo is: " << strDemo << endl;
- return 0;
- }
编译并执行上述代码,结果如下:
这里介绍两种常见的 int 类型转换为 string 类的方法,示例代码如下:
- #include <string>
- #include <iostream>
- #include <sstream>
- using namespace std;
- int main()
- {
- // 方法1
- int nNum1 = 123;
- stringstream ss;
- ss << nNum1;
- string strTest1 = ss.str();
- cout << "strTest1 is: " << strTest1 << endl;
- /*
- string strTest2;
- strTest2 << ss; // stringstream 未定义 << 操作符,故此句报错
- cout << "strTest2 is: " << strTest2 << endl;
- */
- string strTest3;
- ss >> strTest3;
- cout << "strTest3 is: " << strTest3 << endl;
- // 方法2
- int nNum2 = 456;
- string strTest4;
- strTest4 = to_string(nNum2); // C++11 标准
- cout << "strTest4 is: " << strTest4 << endl;
- return 0;
- }
编译并执行上述代码,结果如下:
联系客服