C++中string的用法和例子(1) 插入 截取子字符串 删除

来源:互联网 发布:扫街软件 编辑:程序博客网 时间:2024/06/07 04:05

string是C++标准库的一个重要的部分,主要用于字符串处理。可以使用输入输出流方式直接进行操作,也可以通过文件等手段进行操作。


库想使用string,先要加入头文件

#include<string>


字符串的声明:

string s;   //声明string对象string ss[10];  //string数组

字符串的初始化:

string s;//默认初始化,一个空字符串string s1("ssss");//s1是字面值“ssss”的副本string s2(s1);//s2是s1的副本string s3 = s2;//s3是s2的副本string s4(10, 'c');//把s4初始化string s5 = "hiya";//拷贝初始化string s6 = string(10, 'c');//拷贝初始化,生成一个初始化好的对象,拷贝给s6

字符串截取的三种方法:

(1)保存为char型数组,然后从数组中选择元素个数

char cs[] = "123456";    //char风格字符串string s7 = string(cs, 3);  //复制前三个字符到cs中

(2)利用string的构造函数 截取字符串中N个字符

string s8 = "asac";string s9 = string(s8, 2);   //拷贝前两个字符cout << s9 << endl;


(3)截取中间的子字符串:

string s10 = "abcdefghijk";string s11 = string(s10, 0, 2);  //从下标0开始 拷贝下标为0和1的两个部分cout << s11 << endl;


(4)利用substr()函数

string s12 = "abcdefgh";string s12_1 = s12.substr(4);  //返回以下标4位置元素为开头的子字符串cout << s12_1 << endl;string s12_2 = s12.substr(1, 3); //返回下标为1 2 3对应元素组成的字符串 cout << s12_2 << endl;

Insert操作:

#include <bits/stdc++.h>using namespace std;int main(){    ios::sync_with_stdio(false);    string str="to be question";    string str2="the ";    string str3="or not to be";    string::iterator it;    //s.insert(pos,str)//在s的pos位置插入str    str.insert(6,str2);                 // to be the question    //s.insert(pos,str,a,n)在s的pos位置插入str中插入位置a到后面的n个字符    str.insert(6,str3,3,4);             // to be not the question    //s.insert(pos,cstr,n)//在pos位置插入cstr字符串从开始到后面的n个字符    str.insert(10,"that is cool",8);    // to be not that is the question    //s.insert(pos,cstr)在s的pos位置插入cstr    str.insert(10,"to be ");            // to be not to be that is the question    //s.insert(pos,n,ch)在s.pos位置上面插入n个ch    str.insert(15,1,':');               // to be not to be: that is the question    //s.insert(s.it,ch)在s的it指向位置前面插入一个字符ch,返回新插入的位置的迭代器    it = str.insert(str.begin()+5,','); // to be, not to be: that is the question    //s.insert(s.it,n,ch)//在s的it所指向位置的前面插入n个ch    str.insert (str.end(),3,'.');       // to be, not to be: that is the question...    //s.insert(it,str.ita,str.itb)在it所指向的位置的前面插入[ita,itb)的字符串    str.insert (it+2,str3.begin(),str3.begin()+3); // to be, or not to be: that is the question...    return 0;}

ERASE操作:
指定pos和len,其中pos是起始位置,pos以及后面len-1个字符都删除(左闭右开)
迭代器范围,删除这一范围的字符串,范围左闭右开
#include <iostream>#include <string>int main (){  std::string str ("This is an example sentence.");  std::cout << str << '\n';                          // "This is an example sentence."  str.erase (10,8);       //            ^^^^^^^^  //直接指定删除的字符串位置第十个后面的8个字符  std::cout << str << '\n';                            // "This is an sentence."  str.erase (str.begin()+9);//           ^  //删除迭代器指向的字符  std::cout << str << '\n';                            // "This is a sentence."                            //       ^^^^^  str.erase (str.begin()+5, str.end()-9);  //删除迭代器范围的字符  std::cout << str << '\n';                            // "This sentence."  return 0;}












原创粉丝点击