C++中的std::string类中常用的成员方法详解

总体概述:

std::string类是C++标准库中提供的用于处理字符串的,它包含了很多成员函数,下面是几个常用的成员方法介绍:

构造函数:

  1. string():默认的构造函数,创建一个空字符串;
  2. string(const string& str):复制构造函数,根据已有的字符串创建一个新的字符串;
  3. string(const char* s):使用C风格的字符串(以null结尾),创建一个新的字符串;
  4. string(size_t n, char c):创建一个包含n个字符c的字符串。

赋值和拷贝:

  1. operator=:将一个字符串赋值给另一个字符串;
  2. assign():使用新的内容替换现有字符串的内容。

字符串操作:

  1. size()、length():返回字符串的长度;
  2. empty():检查字符串是否为空;
  3. clear():清除字符串的内容;
  4. +=、append():将字符串或字符追加到原字符串的末尾;
  5. push_back():在字符串末尾添加一个字符;
  6. insert():在指定位置插入字符或字符串;
  7. erase():删除指定范围内的字符;
  8. replace():用新字符串替换指定范围内的字符。

字符串比较:

  1. compare():比较两个字符串的大小;

注意:也可以使用 == , !=, < ,<=,>,>=等符号来比较两个字符串是否相等或者大小关系。

字符串查找和提取:

  1. find():在字符串中从前向后查找子串;
  2. rfind():在字符串中从后向前查找子串;
  3. substr():提取指定位置或范围的子字符串。

字符串转换:

  1. c_str():返回以C风格的字符数组(以null结尾,const char*);
  2. data():返回指向字符串的第一个字符的指针。

其他操作:

  1. swap():交换两个字符串的内容;
  2. at(),operator[]:访问指定索引处的字符。

常用方法的详细介绍:

erase()函数:用于删除字符串中的字符,它有多个重载形式,可以通过指定位置或范围来删除字符。

代码示例:

std::string str = "Hello,world!";

str.erase(1, 2);  // 删除从索引位置1开始的2个字符

std::cout << str << std::endl;  // 输出: Hlo,world!



std::string str2 = "Hello,world!";

str2.erase(7);  //删除从位置7开始后面的所有字符

std::cout << str2 << std::endl;  // 输出: Hello,w
  • length()函数:用于获取字符串的长度,即字符串的字符数。

代码示例:

std::string str = "Hello";

std::cout << str.length() << std::endl;  // 输出: 5

注意:length()函数返回一个无符号整形类型(size_t)的值,表示字符串的长度

  • rbegin()函数:它返回一个反向迭代器,指向字符串的最后一个字符(即逆向的开头)。

代码示例:

std::string str = "Hello";

auto it = str.rbegin();

std::cout << *it << std::endl;  // 输出: o

注意:rbegin()函数可以和其他迭代器操作一起使用,如使用循环逆序访问字符串的字符。

  • substr()函数:用于提取字符串的子串,接受一个起始位置参数和一个可选的长度参数;如果只传入起始位置,则提取从该位置开始到字符串结尾的子串;如果同时指定了长度,则提取指定长度的子串。

代码示例:

std::string str = "Hello,world!";

std::string sub1 = str.substr(6);      // 从索引位置6开始提取子串

std::cout << sub1 << std::endl;        // 输出: world!

std::string sub2 = str.substr(0, 5);   // 从索引位置0开始,提取长度为5的子串

std::cout << sub2 << std::endl;        // 输出: Hello

注意:substr()函数返回的是一个新的std::string对象,表示原字符串中提取出的子串。

  • insert()函数:用于在指定位置插入字符或字符串。

该函数有多个重载:

*string &insert(int p0, const char s);                   //在p0位置插入字符串s

string &insert(int p0, const char *s, int n);            //在p0位置插入字符串s的前n个字符

string &insert(int p0,const string &s);                  //在p0位置插入字符串s

string &insert(int p0,const string &s, int pos, int n);  
//在p0位置插入字符串s从pos开始的连续n个字符

string &insert(int p0, int n, char c);                   //在p0处插入n个字符c

iterator insert(iterator it, char c);                                                      //在it处插入字符c,返回插入后迭代器的位置

void insert(iterator it, const_iterator first, const_iteratorlast);      
//在it处插入从first开始至last-1的所有字符

void insert(iterator it, int n, char c);                 //在it处插入n个字符c
  • replace()函数:用新字符串替换指定范围内的字符。

用法1:用“str”替换指定字符串从起始位置pos开始,长度为len的字符:

std::string input="qwer&&qwer";

input=input.replace(input.find("&"),1,"1"); //将input中第一次出现“&”的位置替换成“1”,输出:qwer1&qwer

用法2:用“str”替换迭代器起始位置到结束位置的字符:

std::string input="qwer&&qwer";

input=input.replace(input.begin(),input.begin()+6,"1"); //将input从起始位置开始的6个字符替换成“1”,输出:1qwer

用法3:通过find相关函数查找将需要被替换的字符替换为指定字符:

std::string str="hello,world,";

std::string input = str.replace(str.find_last_of(","), 1, "\n"); //将str字符串中的最后一个“,”替换为换行符,输出:hello,world(换行符)

以上只是 std::string 类的一些常用成员函数,还有许多其他成员函数可供使用,可以根据具体需求查阅 C++ 标准库文档来获取更详细的信息和用法示例。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C++std::string是一个用于处理字符串的标准库类。它具有多种初始化方式,可以创建空字符串、包含指定内容的字符串等。例如,可以使用以下方式进行字符串的初始化: - 使用默认构造函数创建一个空字符串:std::string str; - 使用括号初始化语法创建一个包含指定内容的字符串:std::string str("hello"); - 使用赋值语句创建一个包含指定内容的字符串:std::string str = "world"; std::string还支持字符串的连接操作。可以使用加号运算符或者将字符串直接连接起来实现连接操作。例如: - 使用加号运算符连接两个字符串:std::string str1 = "hello"; std::string str2 = "world"; std::string str3 = str1 + ", " + str2; // str3 等于 "hello, world" 此外,std::string还提供了一些用于字符串处理的函数和方法。例如,可以使用remove_if函数结合迭代器来移除字符串的特定字符或字符范围。例如: - 使用std::remove_if函数移除字符串的空格字符: std::string name = "John Doe"; std::string::iterator newend = std::remove_if(name.begin(), name.end(), iswhitespace); 此处的iswhitespace是一个判断字符是否为空格的谓词函数。 总结起来,C++std::string类提供了多种初始化方式、字符串连接操作以及一些用于字符串处理的函数和方法。通过这些功能,我们可以方便地进行字符串的操作和处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [C++ 基础: std::string](https://blog.csdn.net/weixin_39568531/article/details/129657195)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [C++ 标准程序库std::string 详解](https://blog.csdn.net/CHYabc123456hh/article/details/108879371)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

倔强de番茄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值