string

string

c++字符串,本质上是一个类

封装了很多成员方法:

查找copy,查找find,删除delete 替换 replace 插入insert

构造函数:

  1. string();
  2. string(const char *s); //使用字符串初始化
  3. string (const string & str);
  4. string (int n,char c);//使用n个字符初始化
    string s1;
    const char * str="hello world";
    string s2(str);
    cout<<"s2="<<s2<<endl;
    string s3(s2);
    cout<<"s3="<<s3<<endl;
    string s4(10,'a');
    cout<<"s4="<<endl;

赋值操作:

给string字符串进行赋值

赋值函数原型

string& operator=(const char* s);
string& operator=(const string & s);
string& operator=(char c);
string& assign(const char * s);
string& assign(const char *s,int n);//把字符串s的前n个字符赋值给当前的字符串
string& assign(cosnt string &S);
sting & assign(int n,char c); //用n个字符c赋给当前字符串
   string s1;
   s1="hello";
   string s2;
   s2=s1;
   cout<<"s2="<<s2<<endl;

    string s3;
    s3.assign("hello c++");
    cout<<"s3="<<s3<<endl;
    string s4;
    s4.assign("hello world",5);
    cout<<"s4="<<s4<<endl;
    string s5;
    s5.assign(s3);
    cout<<"s3="<<s3<<endl;
    string s6;
    s6.assign(5,'b');
    cout<<"s6="<<s6<<endl;

字符串拼接:

在字符串末尾拼接字符串 注意:空格也是一个字符

函数原型:

string& operator+=(const char* str);
string& operator+=(const char c);
string& operator+=(const string& str);
stirng& append(const char *s);
string& append(const char* s,int n);
string& append(const string& s);
string& append(const string& s,int pos,int n);//把字符串从pos位置开始n个字符拼接上
   string  s1;
   s1="i";
   s1+=" love play game";
   cout<<"s1="<<s1<<endl;
    string s2=" dnf lol";
    s1+=s2;
    cout<<"s1="<<s1<<endl;

    string s3;
    s3="i";
    s3.append(" love");
    cout<<"s3="<<s3<<endl;
    s3.append(" gamecent",5);
    cout<<"s3="<<s3<<endl;
    s3.append(s2);
    cout<<"s3="<<s3<<endl;

string查找和替换

功能描述:

查找:查找指定字符串是否存在

替换:在指定的位置替换字符串

int find(const string& str,int pos=0)const; //查找str第一次出现位置,从pos开始查找
int find(const char* s,int pos=0)const; 
int find(const char* s,int pos,int n)const; //从pos位置查找s的前n个字符
int find(const char c,int pos=0)const; //查找字符c出现第一次的位置


int rfind(const string& str,int pos=npos)const;// 查找str最后一次位置,从npos开始
int rfind(const char* s,int pos=npos)const;
int rfind(const char* s,int pos,int n)const;//从pos查找s的前n个字符最后一次位置
int rfind(const char c,int pos=0)const;


string& replace(int pos,int n,const string& str);//替换从pos开始n个字符为字符串str
string& replace(int pos,int n,const char* s); 

查找到则返回字符位置,没有则返回-1

rfind与find的区别:

rfind从右向左,find从左到右

同样是返回下标位置,一般用find就可以了

	string s1="abcdefg";
   int res=s1.find("de");
    cout<<"res="<<res<<endl;
    res=s1.find("df");
    cout<<"res="<<res<<endl;
    

    string s2=s1;
    int res1=s2.rfind("a");
    cout<<"res1="<<res1<<endl;

    s1.replace(1,3,"lol");
    cout<<"s1="<<s1<<endl;

字符串的比较

字符串之间的比较

比较方式:对ASCll码进行对比

= 返回 0

> 返回 大于多少

< 返回 小于多少

函数原型:

int compare(const string & s)const;
int compare(const char* s)cosnt;
  string s1="hello";
   string s2="hello";
   int res=s1.compare(s2);
   cout<<"res="<<res<<endl;

   s2="hello world";
   int res2=s1.compare(s2);
   cout<<"res2="<<res2<<endl;

   s2="hel";
   int res3=s1.compare(s2);
   cout<<"res3="<<res3<<endl;

字符存取

注意:是对单个字符进行存取

//string 中单个字符存取方式有两种
char& operator[](int n);    //通过[]方式取字符
char& at(int n);  // 通过at方法获取字符
    string s1="hello world";

    for(int i=0;i<s1.size();i++)
    {
        cout<<s1[i]<<" ";
    }
    cout<<"\n";

    for(int i=0;i<s1.size();i++)
    {
        cout<<s1.at(i)<<" ";
    }
    cout<<'\n';

插入和删除

对string字符串进行插入和删除字符操作

函数原型:

string& insert(int pos,const char* s);//插入字符串、
string& insert(int pos,const string& str);
string& insert(int pos,int n,char c); //在指定位置插入n个字符c
string& erase(int pos,int n=npos);//删除从pos开始的n个字符
    string s1="hello";
    s1.insert(5," world");
    cout<<"s1="<<s1<<endl;

    string s2="123hello";
    s2.erase(0,3);
    cout<<"s2="<<s2<<endl;

注意:插入删除都是从零开始算

string字串

从字符串中获取想要的字串

函数原型:

string substr(int pos=0,int n=npos)const;//返回由pos开始的n个字符组成的字符串
	string s1="hello";
    string s2=s1.substr(0,2);
    cout<<"s2="<<s2<<endl;
    
   //获取邮箱的用户名
    string email="1440426767@qq.com";
    int pos=email.find("@");
    string str=email.substr(0,pos);
    cout<<"str="<<str<<endl;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值