string篇

目录

构造函数原型:

赋值重载函数  =  assign

拼接函数原型 += append

查找和替换函数原型  find replace

字符串比较函数 compare

字符存取 [] at

插入和删除字符串 insert erase

获取子串 substr


string本质使一个类,类内封装了char *(字符串指针)

构造函数原型:

   string();  //空字符串
   string(const char* s);  //通过字符串初始化
   string(const string& str);  //(构造函数)通过string对象实例化另一个string对象
   string(int n,char c);  //使用n个字符初始化

赋值重载函数  =  assign

string& operator=(const char* s);//char*类型字符串赋值给当前的字符串
string& operator=(const string &s);//把字符串s赋给当前的字符串
string& operator=(char C): //字符赋值给当前的字符串
string& assign(const char *S);//把字符串s赋给当前的字符串
string& assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串
string& assign(const string &s); //把字符串s赋给当前字符串
string& assign(int n,char c);//用n个字符C赋给当前字符串

拼接函数原型 += append

string& operator+=(const char* str);//重载+=操作符 (参数为字符串)

string& operator+-(const char C);//重载+=操作符 (参数为字符)

string& operator+=(const string& str); .//重载+=操作符  (参数为对象)

string& append(const char *s);//把字符串s连接到当前字符串结尾 (参数为字符串)

string& append(const char *s,int n);//把字符串s的前n个字符连接到当前字符串结尾 

string& append(const string &s);//同operator+=(const string& str) (参数为对象)

string& append(const string &s, int pos, int n); //字符串s中从pos开始的n个字符连接到字符串结尾 
 

查找和替换函数原型  find replace

(find从左向右查,rfind从右向左查,find查不到返回-1)

int find(const string& str,int pos=0) const;//查找str第1次出现位置,从pos开始查找

int find(const char* s, int pos = 0) const;//查找s第1次出现位置从pos开始查找

int find(const char* s, int pos, int n) const;//从pos位置查找s的前n个字符第1次位置

int find(const char C, int pos = 0) const;//查找字符c第1次出现位置

int rfind(const string& str, int pos=npos) const; //查找str最后1次位置从pos开始查找

int rfind(const char* s, int pos = npos) const;,//查找s最后1次出现位置从pos开始查找

int rfind(const char* s, int pos, int n) const;//从pos查找s的前n个字符最后1次位置

int rfindconst char C, int pos = 0) const;//查找字符c最后1次出现位置

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

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

字符串比较函数 compare

(按ascII值逐位比较,不受长度影响,=返回0,>返回1,<返回-1)

int compare(const string &s) const; //与字符串s比较
int compare(const char *s) const;//与字符串s比较

字符存取 [] at

char& operator[ ] (int a); //通过 [ ] 方式取字符  例:string a ; a[ 1 ] = 'a' ; cout << a [ 1 ] ;

cahr& at(int n); //通过at方法取字符 例:string a ;  a.at ( 1 )= 'a' ; cout << a.at ( 2 ) ;

插入和删除字符串 insert erase

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个字符

获取子串 substr

目录

构造函数原型:

赋值重载函数  =  assign

拼接函数原型 += append

查找和替换函数原型  find replace

字符串比较函数 compare

字符存取 [] at

插入和删除字符串 insert erase

获取子串 substr


string substr (int pos = 0 , int n = npos) const ; //返回从pos开始的n个字符组成的字符串

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值