打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C++中的string类用法简介

本文主要介绍C++中的string类的常见用法。

1. 概述

string是C++标准库的一个重要的部分,主要用于字符串处理。可以使用输入输出流方式直接进行string操作,也可以通过文件等手段进行string操作。同时,C++的算法库对string类也有着很好的支持,并且string类还和c语言的字符串之间有着良好的接口。

2. 常见用法

2.1 string转换为char*

方法一:使用 c_str() 方法,代码(stringsimple.cpp)如下:

  1. #include <string>
  2. #include <iostream>
  3. #include <stdio.h>
  4. using namespace std;
  5. int main()
  6. {
  7.     string strOutput = "Hello World";
  8.     cout << "[cout] strOutput is: " << strOutput << endl;
  9.     // string 转换为 char*
  10.     const char* pszOutput = strOutput.c_str();
  11.     printf("[printf] strOutput is: %s\n", pszOutput);
  12.     return 0;
  13. }

编译并执行上述代码,结果如下:

上述代码执行结果说明:

  • cout 可直接输出 string 类的对象的内容;
  • 使用 c_str() 方法转换 string 类型到 char* 类型时,需要为char*添加 const 关键字;
  • printf() 函数不能直接打印 string 类的对象的内容,可以通过将 string 转换为 char* 类型,再使用 printf() 函数打印。

2.1.1 data()方法与c_str()方法

data()方法与c_str()方法相似,都返回 const char* 类型。两者区别和联系如下:

  • 在C++98版本中,c_str()返回 const char* 类型,返回的字符串会以空字符(null character)结尾;
  • 在C++98版本中,data()返回 const char* 类型,返回的字符串不以空字符(null character)结尾;
  • 在C++11版本中,c_str()与data()用法相同(Both string::data and string::c_str are synonyms and return the same value.)

2.2 计算string长度、string字符串比较

示例代码如下:

  1. #include <string>
  2. #include <iostream>
  3. #define HELLOSTR "Hello World"
  4. using namespace std;
  5. int main()
  6. {
  7. string strOutput = "Hello World";
  8. int nLen = strOutput.length();
  9. cout << "the length of strOutput is: " << nLen << endl;
  10. if (0 == strOutput.compare(HELLOSTR))
  11. {
  12. cout << "strOutput equal with macro HELLOSTR" << endl;
  13. }
  14. return 0;
  15. }

编译并执行上述代码,结果如下:

[root@node1 /opt/liitdar/mydemos/simples]# ./stringsimple2 the length of strOutput is: 11strOutput equal with macro HELLOSTR[root@node1 /opt/liitdar/mydemos/simples]# 

上述代码执行结果说明:

  • string类型可直接使用 length() 方法计算字符串长度,该方法计算结果为字符串的实际长度,如本例中"Hello World"字符串的长度为11;
  • string类型可使用 compare(const string& str) 方法进行字符串比较。

2.3 string对象判空

可使用 empty() 方法对string类型的对象进行判空,如下:

  1. if (str2.empty())
  2. {
  3. cout << "str2 is empty." << endl;
  4. }

2.4 char*、char[]转换为string

将 char*、char[] 转换为 string 类型时,直接进行赋值操作,将 char*、char[] 的变量赋值给 string 对象即可。

说明:这里所说的“赋值”操作,实际上是将 char*、char[] 定义的字符串的首地址赋值给 string 对象了。

示例代码(stringtochar.cpp)如下:

  1. #include <string>
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. const char* pszName = "liitdar";
  7. char pszCamp[] = "alliance";
  8. string strName;
  9. string strCamp;
  10. strName = pszName;
  11. strCamp = pszCamp;
  12. cout << "strName is: " << strName << endl;
  13. cout << "strCamp is: " << strCamp << endl;
  14. return 0;
  15. }

编译并执行上述代码,结果如下:

2.5 string类的find方法

使用string类的find方法,在字符串中检索自字符串是否存在。

2.5.1 用法

用法如下:

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

2.5.2 返回值

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).

2.5.3 示例代码

find方法的示例代码(string_find_test1.cpp)如下:

  1. #include <string>
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. // 待检索的字符串
  7. string strOutput = "|0|1|2|";
  8. // 需要检索的子串
  9. string strObj = "|1|";
  10. // 子串位于字符串中的位置
  11. size_t nLoc = strOutput.find(strObj);
  12. // 如果检索到子串在字符串中,则打印子串的位置
  13. if (nLoc != string::npos)
  14. {
  15. cout << "nLoc is: " << nLoc << endl;
  16. }
  17. return 0;
  18. }

编译并执行上述代码,结果如下:

2.6 string类的insert方法

使用string类的insert方法,向字符串中插入字符(串)。官方的定义如下:

Inserts additional characters into the string right before the character indicated by pos (or p).

2.6.1 用法

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);

2.6.2 示例代码

insert方法的示例代码(string_insert_test1.cpp)如下:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. string strDemo = "I am";
  7. strDemo.insert(4, " good.");
  8. cout << "strDemo is: " << strDemo << endl;
  9. return 0;
  10. }

编译并执行上述代码,结果如下:

2.7 int类型转为string类的方法

这里介绍两种常见的 int 类型转换为 string 类的方法,示例代码如下:

  1. #include <string>
  2. #include <iostream>
  3. #include <sstream>
  4. using namespace std;
  5. int main()
  6. {
  7. // 方法1
  8. int nNum1 = 123;
  9. stringstream ss;
  10. ss << nNum1;
  11. string strTest1 = ss.str();
  12. cout << "strTest1 is: " << strTest1 << endl;
  13. /*
  14. string strTest2;
  15. strTest2 << ss; // stringstream 未定义 << 操作符,故此句报错
  16. cout << "strTest2 is: " << strTest2 << endl;
  17. */
  18. string strTest3;
  19. ss >> strTest3;
  20. cout << "strTest3 is: " << strTest3 << endl;
  21. // 方法2
  22. int nNum2 = 456;
  23. string strTest4;
  24. strTest4 = to_string(nNum2); // C++11 标准
  25. cout << "strTest4 is: " << strTest4 << endl;
  26. return 0;
  27. }

编译并执行上述代码,结果如下:

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
CString, int, string, char*之间的转换_C语言教程_C++教程_...
VC中format用法 各种转换方式 int 转 CString char* 转 int
string,CString,char*之间的转化
string与char*
宽字符串和标准字符串的互相转换
C++ replace() 函数用法详解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服