STL-string

本文介绍了C++中的字符串操作,包括字符串构造函数、赋值、拼接、查找(包括查找位置和替换操作)、字符串比较以及获取、插入和删除子串的方法,通过实例展示了这些功能的使用。
摘要由CSDN通过智能技术生成
  1. 字符串的拼接
  2. 查找和替换
  3. 比较
  4. 获取
  5. 插入和删除
  6. 字符串的子串 拿来截取一段字符串中的一段
#include<iostream>
#include<vector>
#include<string>
using namespace std;
//string构造函数
void test01()
{
	//string();
	//string(const char* s);
	//string(const string & str);
	//string(int n, char c);
	string s1;  //创建空字符串;
	const char* str = "hello world";
	string s2(str);
	cout << "str = " << s2 << endl;
	string s3(s2);
	cout << "str = " << s3 << endl;
	string s4(10, 'a');
	cout << "str = " << s4 << endl;
}

//string 赋值
void test02() {
	//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赋给当前字符串
	string s1;
	s1 = "hello world";
	cout << "s1 = " << s1 << endl;
	string s2;
	s2 = s1;
	cout << "s2 = " << s2 << endl;
	string s3;
	s3.assign("hello c++");
	cout << "s3 = " << s3 << endl;
	string s4;
	s4.assign("hello c++",5);
	cout << "s4 = " << s4 << endl;
	string s5;
	s5.assign(s4);
	cout << "s5 = " << s5 << endl;
	string s6;
	s6.assign(5, 'x');
	cout << "str6 = " << s6 << endl;

}

//字符串拼接
void test03() {
	//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个字符连接到字符串结尾
	string s1 = "我";
	s1 += " ai xiaobai";
	cout << s1 << endl;
	s1 += ':';
	cout << s1 << endl;
	string s2="打王者荣耀";
	s1 += s2;
	cout << s1 << endl;
	string s3="我爱玩";
	s3.append("你爱玩吗");
	cout << s3 << endl;
	s3.append("如果你爱玩可以一起玩", 10);  //一个中文字两个字符
	cout << s3 << endl;
	s3.append("如果你爱玩可以一起玩", 10, 10);//从下标10开始,截取10个字符到末尾
	cout << s3 << endl; //截取了“可以一起玩”

}

//字符串查找和替换
void test04()
{
	//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
	string str1 = "abcdefgh";
	int pos = str1.find("de");
	if (pos == -1)
	{
		cout << "bu存在" << endl;
	}
	else
	{
		cout << "存在" << endl;

	}
	pos = str1.rfind("de");
	cout << "pos = " << pos << endl;
	string str2 = "abcdefg";
	str2.replace(1, 3, "111");
	cout << str2 << endl;
}

//字符串比较
void test05()
{
	string s1 = "我是小白的主ren";
	string s2 = "我是小白的主rn";
	int ret = s1.compare(s2);
	if (ret == 0)
	{
		cout << "s1 = s2" << endl;
	}
	else if (ret == 1)
	{
		cout << "s1 > s2" << endl;
	}
	else
	{
		cout << "s1 < s2" << endl;

	}
}

//字符串获取
void test06()
{
	string s1 = "abcd4";
	//第一种用[]
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << " " ;
	}
	cout << endl;
	//第二种用at
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1.at(i) << " ";
	}
	cout << endl;
	//修改字符串
	s1[0] = 'z';
	s1.at(1)='h';
	cout << s1 << endl;
}

//字符串插入和删除


void test07()
{
	string s1 = "hello ";
	s1.insert(1, " world");
	cout << s1;

	//删除
	s1.erase(1, 3); //从索引1位置开始
	cout << s1 << endl;
}

//字符串子串  //截取  
void test08()
{
	string str1 = "abcdef";
	string substr = str1.substr(1, 3);
	cout << "substr = " << substr << endl;

	//实用案例 截取email地址
	string email = "993667249@qq.com";
	int pos = email.find("@");
	string username = email.substr(0, pos); //返回由pos开始的n个字符组成的字符串
	cout << "username: " << username << endl;
}
int main()
{
	test01(); //string构造函数
	cout << "-----------" << endl;
	test02();//string 赋值
	test03();//string 字符串拼接
	test04();
	test05();
	test06();
	test07();
	test08();
	return 0;
}
  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值