2021-03-17-C++学习之14-string

一、string基本概念

  • 本质:string 是C++风格的字符串,而 string 本质上是一个类。
  • stringchar* 区别:
    • char* 是一个指针
    • string 是一个类,类内部封装了char* ,管理这个字符串,是一个 char* 型的容器。
  • 特点:
    • string 类内部封装了很多成员方法。
    • 例如:查找find、拷贝copy、删除delete、插入insert
    • string管理char* 所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责。

二、string构造函数
构造函数原型:

  • string(); //创建一个空的字符串,例如:string str;
  • string(const char* s); //使用字符串初始化
  • string(const string& str); //使用一个string对象初始化另一个string对象
  • string(int n, charc); //使用n个字符c初始化

代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件

//构造字符串
void test01()
{
	string s1; //默认构造

	//string(const char* s);
	const char* str = "hello world";
	string s2(str);
	cout << "s2 = " << s2 << endl;

	//string(const string& str);
	string s3(s2);
	cout << "s3 = " << s3 << endl;

	//string(int n, charc);
	string s4(10, 'a');
	cout << "s4 = " << s4 << endl;
}

int main()
{
	test01();
	
	system("pause");
	return 0;
}

输出:

s2 = hello world
s3 = hello world
s4 = aaaaaaaaaa
请按任意键继续. . .

三、string赋值操作
功能:给string字符串进行赋值
赋值的函数模型:

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

代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件

//字符串赋值
void test01()
{
	//char* 类型字符串 赋值给当前的字符串
	string str1;
	str1 = "hello world";
	cout << "str1 = " << str1 << endl;
	//把字符串s赋给当前的字符串
	string str2;
	str2 = str1;
	cout << "str2 = " << str2 << endl;
	//字符赋给当前的字符串
	string str3;
	str3 = 'a';
	cout << "str3 = " << str3 << endl;
	//assign -- char* 类型字符串 赋值给当前的字符串
	string str4;
	str4.assign("hello C++");
	cout << "str4 = " << str4 << endl;
	//把字符串s的前n个字符赋给当前的字符串
	string str5;
	str5.assign("hello world", 5);
	cout << "str5 = " << str5 << endl;
	//assign -- 把字符串s赋给当前字符串
	string str6;
	str6.assign(str5);
	cout << "str6 = " << str6 << endl;
	//assign -- 用n个字符c赋给当前字符串
	string str7;
	str7.assign(10,'a');
	cout << "str7 = " << str7 << endl;
}

int main()
{
	test01();
	
	system("pause");
	return 0;
}

输出:

str1 = hello world
str2 = hello world
str3 = a
str4 = hello C++
str5 = hello
str6 = hello
str7 = aaaaaaaaaa
请按任意键继续. . .

四、string字符串拼接
功能描述:在字符串末尾拼接字符串
函数原型:
在这里插入图片描述
代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件

//字符串拼接 -- 会改变原字符串
void test01()
{
	string str1 = "我";
	str1 += "爱玩游戏";
	cout << "str1 = " << str1 << endl;
	str1 += ":";
	cout << "str1 = " << str1 << endl;
	string str2 = " 和平精英 王者荣耀";
	str1 += str2;
	cout << "str1 = " << str1 << endl;

	string str3 = "I";
	str3.append(" love ");
	cout << "str3 = " << str3 << endl;
	str3.append("game LOL", 4);
	cout << "str3 = " << str3 << endl;
	//str3.append(str2);
	//从第0个开始,截取9个字符,一个汉字两个字符
	//str3.append(str2, 0, 9);  //输出:str3 = I love game 和平精英
	//从编号为9开始,截取9个字符
	str3.append(str2, 9, 9); //输出:str3 = I love game 王者荣耀
	cout << "str3 = " << str3 << endl;
}

int main()
{
	test01();
	
	system("pause");
	return 0;
}

输出:

str1 = 我爱玩游戏
str1 = 我爱玩游戏:
str1 = 我爱玩游戏: 和平精英 王者荣耀
str3 = I love
str3 = I love game
str3 = I love game 王者荣耀
请按任意键继续. . .

五、string 查找和替换
功能描述:

  • 查找指定字符串是否存在
  • 在指定的位置替换字符串

函数原型:
在这里插入图片描述
代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件

//字符串查找
void test01()
{
	//find
	string str1 = "abcdefgde";
	int pos = str1.find("de");
	int pos2 = str1.find("ab");
	cout << "pos = " << pos << endl;//输出:pos = 3,注意编号从0开始
	//cout << "pos2 = " << pos2 << endl; //没有,输出:pos2 = -1
	if (pos2 == -1)
	{
		cout << "未找到字符串" << endl;
	}
	else
	{
		cout << "pos2 = " << pos2 << endl;
	}

	//rfind 和 find 区别
	//rfdind从右往左查找  find 从左往右查找
	int pos3 = str1.rfind("de");
	cout << "pos3 = " << pos3 << endl; //输出:pos3 = 7
}
//字符串与替换
void test02()
{
	string str1 = "abcdefg";
	//从编号为1的字符起,替换3个字符,即替换 bcd 为 1111
	str1.replace(1, 3, "1111"); //四个 1 都会被替换进去
	cout << "str1 = " << str1 << endl; //输出:str1 = a1111efg
}
int main()
{
	test01();
	test02();
	
	system("pause");
	return 0;
}

输出:

pos = 3
pos2 = 0
pos3 = 7
str1 = a1111efg
请按任意键继续. . .

总结:
在这里插入图片描述
六、string 字符串比较
比较方式:

  • 按照字符的ASCII码进行逐个对比,三种结果:
    • 返回 0 两个字符串相等
    • 返回 1 第一个字符串大于第二个字符串
    • 返回 -1 第一个字符串小于第二个字符串
  • 函数原型:
    • int compare(const string &s) const; //与字符串s比较
    • int compare(const char* s) const; //与字符串s比较

代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件

//字符串比较
void test01()
{
	string str1 = "mello";
	string str2 = "hello1";
	if (str1.compare(str2) == 0)
	{
		cout << "str1 = str2" << endl;
	}
	else if(str1.compare(str2) == 1)
	{
		cout << "str1 > str2" << endl;
	}
	else if (str1.compare(str2) == -1)
	{
		cout << "str1 < str2" << endl;
	}
}

int main()
{
	test01();
	
	system("pause");
	return 0;
}

输出:

str1 > str2
请按任意键继续. . .

七、string 字符存取
string中单个字符存取方式有两种:

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

代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件

//字符串字符存取
void test01()
{
	string str = "hello";
	//cout << "str = " << str << endl;
	//读取
	//1、通过[]方式访问单个字符
	for (int i = 0; i < str.size(); i++)
	{
		cout << str[i] << " ";
	}
	cout << endl;
	//2、通过at方式访问单个字符
	for (int i = 0; i < str.size(); i++)
	{
		cout << str.at(i) << " ";
	}
	cout << endl;

	//修改单个字符
	str[0] = 'x';
	cout << "str = " << str << endl;
	str.at(1) = 'x';
	cout << "str = " << str << endl;
}

int main()
{
	test01();
	
	system("pause");
	return 0;
}

输出:

h e l l o
h e l l o
str = xello
str = xxllo
请按任意键继续. . .

八、string 插入和删除
函数原型:
在这里插入图片描述
代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件

//字符串字符插入与删除
void test01()
{
	string str = "hello";
	//插入,从编号为1位置开始插入3个字符111
	str.insert(1, "111");
	cout << "str = " << str << endl; //输出:str = h111ello
	//删除,从编号为1位置开始删除3个字符
	str.erase(1, 3);
	cout << "str = " << str << endl; //输出:str = hello
}

int main()
{
	test01();
	
	system("pause");
	return 0;
}

输出:

str = h111ello
str = hello
请按任意键继续. . .

九、string子串获取
函数原型:

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

代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件

//字符串字截取
void test01()
{
	string str = "abcdef";
	string subStr = str.substr(1, 3);
	cout << "subStr = " << subStr << endl; //输出:subStr = bcd
}
void test02()
{
	string email = "zhangsan@sina.com";
	//从邮件地址中 获取 用户名信息 zhangsan
	int pos = email.find('@');
	string userName = email.substr(0, pos);
	cout << "userName = " << userName << endl;
}

int main()
{
	test01();
	test02();
	
	system("pause");
	return 0;
}

输出:

subStr = bcd
userName = zhangsan
请按任意键继续. . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

努力学习的代码小白

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

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

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

打赏作者

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

抵扣说明:

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

余额充值