C++中string类的方法总结

string类与C语言中的以 '/0' 结尾的字符串不同, string类的本质上是以字符作为元素的vector特化版本;不存在0字符结尾这个概念,能装入'\0'这种数据

本文介绍string类的常用方法,方便日后复习:

1.常用构造函数

string s1;//定义字符串,调用默认构造函数,生成空字符串 s1 = "";

string s2("abcde"); //定义字符串,并调用string(char *s)构造函数来进行初始化,生成s2 = "abcde"字符串

string s3(3,'a');//定义字符串s3,并调用string(int n,char c)构造函数来进行初始化,生成由 n个字符'c'组成的字符串,比如 s3 = "aaa";通常用来为已知大小的字符串分配空间

string s4 = "rtrt";//定义字符串s4,并调用拷贝构造函数string(string &s),生成字符串s4 = "rtrt"

2.获取字符串的长度

string s = "abcdef";

//string::length() 和 string::size()两者作用相同,返回字符串中包含的字符数
s.length();  //字符串所含字符数为6
s.size();   //字符串所含字符数为6

s.empty();//判断字符串s是否为空,为空则返回true,不为空则返回false

3.单个字符和字符串的拼接

//第一种方法是通过重载 + 操作符
//即通过 + 既可以连接string对象,又可以连接C语言中的字符串 

//string &operator+=(const string &s);  //把字符串s连接到当前字符串结尾
string s1 = "abc";
string s2 = "def";
string s3 = s1 +s2; //输出s3 = "abcdef"


//string &operator+=(const char *s);//把字符串s连接到当前字符串结尾
char *s4 = "ghi";
string s5 = s3 + s4; //输出s5 = "abcdefghi" 



//第二种方法是通过string类的append方法
string &append(const char *s);    //把字符串s连接到当前字符串结尾

string a1 = "abc";
char* a2 = "def";
a2.append(a1); //输出a2为"abcdef"

string &append(const string &s);   //同operator+=()

string &append(int n, char c);   //在当前字符串结尾添加n个字符c

char a3 = 'g';
a2.append(3,g);//输出a2为"abcdefggg"

4.单个字符和字符串的插入

//在pos位置插入字符串s,字符串有两种表示形式,char*型和string型
string &insert(int pos, const char *s);
string &insert(int pos, const string &s);


string s1 = "abcdef";
string s2 = "yy";
s1.insert(2,s2);//在s1的下标为2的位置处插入字符串s2;输出结果为"abyycdef"


string &insert(int pos, int n, char c);  //在pos位置 插入n个字符c

string a1 = "abc";
char a2 = 'y';
a1.insert(0,3,a2);在a1中下标为0的地方插入3个字符a2,输出结果为a1 = "yyyabc"

5.获取字符串的子串

//返回由pos开始的n个字符组成的子字符串
string substr(int pos=0, int n=npos) const;//包含下标pos处的字符,总字符为npos的子串    

string s1 = "abcdefghi";
string s2 = s1.substr(1,3);//获取从1开始,总字符个数为3的子串
//输出结果为  s2 = "bcd"

6.字符串的查找

//在字符串中查找特定的字符或字符串
int find(char c,int pos=0) const;  //从pos开始查找字符c在当前字符串的位置 
int find(const char *s, int pos=0) const;  //从pos开始查找字符串s在当前字符串的位置
int find(const string &s, int pos=0) const;  //从pos开始查找字符串s在当前字符串中的位置
find函数如果查找不到,就返回 string::npos ;找到则返回首字符的下标

string s1 = "abcdefg";
s1.find('d',1);//从下标1处开始找字符d
//查找结果为3,表示在下标3处找到字符d

s1.find("ef",0);//从下标0处开始查找字符串"ef",返回结果为4,表示在下标4处找到字符串"ef"

s1.find("yy",0);//此时返回结果为string::npos

7.字符串的替换

string &replace(int pos, int n, const char *s);//删除从pos开始的n个字符,然后在pos处插入串s
string &replace(int pos, int n, const string &s);  //删除从pos开始的n个字符,然后在pos处插入串s
string s1 = "abcdef";
s1.replace(1,3,"yy");//将下标1-3处的字符串替换为"yy"
//输出结果为 "ayyef"

8.字符串子串或单字符的删除

iterator erase(iterator first, iterator last);//删除[first,last)之间的所有字符,返回删除后迭代器的位置,注意右边为开区间
iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置
string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串
string s1 = "abcdefgh";
s1.erase(s1.begin(),s1.begin()+2);//删除"ab"
//输出结果为"cdefgh"

string s2 = "abcdefg";
s2.erase(s2.begin());//删除字符'a'
//输出结果为"bcdefg"

string s3 = "abcdef";
s3.erase(2,3);//删除s3下标从2开始的3个字符,即"cde"
//输出结果为"abf"

9.字符串的比较

int compare(const string &s) const;  //与字符串s比较
int compare(const char *s) const;   //与字符串s比较
//compare函数在>时返回 1,<时返回 -1,==时返回 0。比较区分大小写,比较时参考字典顺序,排越前面的越小。大写的A比小写的a小。

10.string类的输入输出操作

string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作

string s1 = "abcdef";
cout << s1 << endl; //输出s1 = "abcdef"

string s2;
cin >> s2;//在显示串口从键盘键入"yyy"
cout << s2 <<endl;//输出s2 = "yyy"

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值