string容器(字符串)

string基本概念。
string是C++风格的字符串,而string本质是一个类。包含于头文件#include <iostream>中

特点:
string类内部封装了很多成员方法。
例如:构造函数,查找,追加,插入,删除...
string通过char*来管理内存的,不用担心赋值越界以及取值越界等问题。

#include <iostream>
using namespace std;

int main(int argc,char *argv[])
{
	string s1;  //默认构造

	const char *str = "helloworld";
	string s2(str);                    //带参构造
	cout << "s2 = " << s2 << endl;     //s2 = helloworld

	string s3(s2);                     //拷贝
	cout << "s3 = " << s3 << endl;     //s3 = helloworld
    
	string s4(10,'a');                 //10个'a'字符赋值给当前字符串
	cout << "s4 = " << s4 << endl;     //s4 = aaaaaaaaaa

	string s5;
	s5 = s4;                           //拷贝
	cout << "s5 = " << s5 << endl;     //s5 = aaaaaaaaaa

	string s6;
    //s6.assign("hello C++");          //"hello C++"赋值给当前字符串
	s6.assign("hello C++",5);          //"hello C++"的前5个赋值给当前字符串
	cout << "s6 = " << s6 << endl;     //s6 = hello

	string s7 = "hello";
	s7 += "world";                     //字符串拼接
	cout << "s7 = " << s7 << endl;     //s7 = helloworld

	string s8 = "OK";
	s8.append(s7);                     //字符串拼接
	cout << "s8 = " << s8 << endl;     //s8 = OKhelloworld

	string s9 = "abcdefgde";
	int pos = s9.find("de");           //从左边开始找,返回下标
	cout << "pos = " << pos << endl;   //pos = 3

	int pos1 = s9.rfind("de");         //从右边开始找,返回下标
	cout << "pos1 = " << pos1 << endl; //pos1 = 7

	string s10 = "abcdefg";
	s10.replace(1,3,"1111");           //从下标1开始3个字符替换为"1111"
	cout << "s10 = " << s10 << endl;   //s10 = a1111efg

	string s11 = "hello";
	string s12 = "hello";
	if(s11.compare(s12) == 0)                 //字符串比较
	{
		cout << "s11 等于 s12" << endl;       //s11 等于 s12
	}
	else{
		cout << "s11 不等于 s12" << endl;
	}

	string s13 = "hello";
	cout << "s13[3] = " << s13[3] << endl;    //s13[3] = l

	cout << s13.at(3) << endl;                //l

	string s14 = "hello";
	s14.insert(1,"111");                      //从下标1插入字符串"111"
	cout << "s14 = " << s14 << endl;          //s14 = h111ello

	s14.erase(1,3);                           //从下标1开始删除3个字符
	cout << "s14 = " << s14 << endl;          //s14 = hello

	string s15 = "abcdef";
	string str1 = s15.substr(1,3);            //返回从下标1开始的3个字符
	cout << "str1 = " << str1 << endl;        //str1 = bcd

	return 0;
}
  1. 构造函数

string();                  //创建一个空的字符串 例如string str;

string(const char* s);       //使用字符串s初始化

string(const string& str);     //使用一个string对象初始化另外一个string对象。

string(int n,char c);          //使用n个字符c初始化。

赋值操作

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);  //把字符串赋给当前的字符串

string& assign(int n,char c);   //用n个字符c赋值给当前字符串。

字符串拼接

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个字符连接到字符串结尾

查找和替换

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

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

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最后一次位置,从pos开始查找

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

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

int rfind(const char c,int pos = 0) const;//查找字符c最后一次出现位置

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

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

字符串比较

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

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

字符存取

char& operator[](int n);  //通过[]方式取字符

char& at(int n);          //通过at方法获取字符

插入和删除

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子串

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

物の哀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值