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

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












评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值