• 【C++】string 之 substr、insert、erase函数的学习


    前言

    之前两篇文章 我们学习了

    assign、at、append函数
    find、rfind、replace、compare函数

    这些函数。接下来让我们继续学习其他函数

    substr

    两个参数
    pos1,截取的开始位置
    len,截取的子串长度

    作用是在字符串中截取一段长度为len的子串

    下面给出一个例子

    #include
    
    using namespace std;
    
    int main()
    {
    	string str = "Hello World";
    	string ss = str.substr(2, 3);
    	cout << ss;
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    运行结果:
    在这里插入图片描述

    insert

    用法一:

    两个参数
    pos1:插入的位置
    str:要插入的字符串

    下面给出一个例子:

    #include
    
    using namespace std;
    
    int main()
    {
    	string str = "Hello World";
    	string a;
    	a = str.insert(3, "hh");
    	cout << a << endl;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    运行结果:
    在这里插入图片描述

    用法二

    三个参数:
    pos1:要插入的位置
    n:要插入的元素个数
    ch:要插入的元素

    下面给出一个例子:

    #include
    #include
    
    using namespace std;
    
    int main()
    {
    	string str = "Nice To Meet You";
    	string a, b;
    	a = str.insert(0,"Hello ");
    	cout << a << endl;
    	
    	b = str.insert(str.size(), 3, 'h');
    	cout << b << endl;
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    运行结果:
    在这里插入图片描述

    erase

    作用是删除字符串中的字符

    使用erase函数需要包含algorithm头文件

    用法1

    两个参数:
    pos1:下标,即开始删除的位置
    n:删除的元素个数

    下面给出一段代码:

    #include
    
    using namespace std;
    
    int main()
    {
    	string str1 = "Hello World";
    	string a;
    	a = str1.erase(0,3);
    	cout << a << endl << str1 << endl;
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    运行结果:
    在这里插入图片描述

    用法2

    pos:下标,要删除元素的下标

    作用是删除pos位置的元素

    下面给出一段代码:

    #include  
    #include  
    using namespace std;  
      
    int main()  
    {  
     string str1 = "Hello World";  
     string a;  
     //a = str1.substr(0, 1); // 取得要被删除的部分  
     str1.erase(str1.begin()); // 在原字符串上删除这部分
     cout << a << endl << str1 << endl; // 输出结果  
       
     return 0;  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    运行结果:
    在这里插入图片描述

    注意

    当你尝试这么写的时候

    String a;
    a = str1.erase(str1.begin());
    
    • 1
    • 2

    程序会报错。

    因为erase函数会删除给定位置开始到字符串末尾的所有字符,但它并不返回一个新的字符串。而是直接在原字符串上进行修改,并返回对原字符串的引用。

    所以a其实是空的

    用法3

    pos1:删除的起始位置
    pos2:删除的结束位置

    下面给出一段代码:

    #include
    #include
    
    using namespace std;
    
    int main()
    {
    	string str1 = "Hello World";
    	str1.erase(str1.begin() + 1, str1.end() - 1);
    	cout << str1 << endl;
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    运行结果:
    在这里插入图片描述

    结语

    介绍到这里,string中的基本函数就学习完了
    希望你有所收获 我们下篇文章见~

  • 相关阅读:
    网址收藏-技术类
    Spring IoC 的工作流程
    【数据结构与算法分析】0基础带你学数据结构与算法分析09--线索二叉树 (TBT)
    【Element UI】解决 el-dialog 弹框组件设置 custom-class 样式不生效问题
    ElementUI入门到入土
    毫秒级!千万人脸库快速比对,上亿商品图片检索,背后的极速检索用了什么神器?
    达梦相关笔记
    c++ 高效使用vector(面试)
    echarts问题总结2
    【LeetCode每日一题】——561.数组拆分
  • 原文地址:https://blog.csdn.net/cat_with_cat/article/details/133380509