string容器

目录

string函数的构造

string赋值操作

string字符串拼接

string字符串查找和替换

string字符串比较

string字符存取

string插入与删除

string字串


string函数的构造

cpp 复制代码
#include<iostream>
#include<cstring>
using namespace std;
void test01()
{
	string s1;//默认构造

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

	string s3(s2);
	cout << "s3 = " << s3 << endl;

	string s4(10, 'a');
	cout << "s4 = " << s4 << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

string赋值操作

cpp 复制代码
//author:至尊宝
//time:2024.5.5
#include<iostream>
#include<cstring>
using namespace std;
void test01()
{
	string str1;
	str1 = "hello world";
	cout << "str1 = " << str1 << endl;

	string str2;
	str2 = str1;
	cout << "str2 = " << str2 << endl;

	string str3;
	str3 = 'a';
	cout << "str3 = " << str3 << endl;

	string str4;
	str4.assign("hello C++");
	cout << "str4 = " << str4 << endl;

	string str5;
	str5.assign("hello C++", 5);
	cout << "str5 = " << str5 << endl;

	string str6;
	str6.assign(str5);
	cout << "str6 = " << str6 << endl;

	string str7;
	str7.assign(10, 'b');
	cout << "str7 = " << str7 << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

string字符串拼接

cpp 复制代码
//author:至尊宝
//time:2024.5.5
#include<iostream>
#include<cstring>
using namespace std;
void test01()
{
	string str1 = "我";
	str1 += "爱玩游戏";
	cout << "str1 = " << str1 << endl;

	str1 += ':';
	cout << "str1 = " << str1 << endl;

	string str2 = "LOL DNF";
	str1 += str2;
	cout << "str1 = " << str1 << endl;

	string str3 = "I";
	str3.append(" love ");
	cout << "str3 = " << str3 << endl;

	str3.append("game abcde", 4);
	cout << "str3 = " << str3 << endl;

	/*str3.append(str2);
	cout << "str3 = " << str3 << endl;*/

	//截取DNF
	str3 += ' ';
	str3.append(str2, 4, 3);
	cout << "str3 = " << str3 << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

string字符串查找和替换

cpp 复制代码
//author:至尊宝
//time:2024.5.5
#include<iostream>
#include<cstring>
using namespace std;
void test01()
{
	string str1 = "abcdefg";
	int pos = str1.find("deo");
	if (pos == -1)
	{
		cout << "未找到字符串" << endl;
	}
	else
	{
		cout << "找到字符串" << ",pos = " << pos << endl;
	}
	
	//rfind
	pos = str1.rfind("de");
	cout << "pos = " << pos << endl;
	//rfind从右往左查找,find从左往右查找
}
void test02()
{
	string str1 = "abcdefg";
	//从一号位置起3个字符替换为1111
	str1.replace(1, 3, "1111");
	cout << "str1 = " << str1 << endl;
}
int main()
{
	test02();
	system("pause");
	return 0;
}

string字符串比较

cpp 复制代码
//author:至尊宝
//time:2024.5.5
#include<iostream>
#include<cstring>
using namespace std;
void test01()
{
	string str1 = "xello";
	string str2 = "hello";
	if (str1.compare(str2) == 0)
	{
		cout << "str1等于str2" << endl;
	}
	else if (str1.compare(str2) > 0)
	{
		cout << "str1大于str2" << endl;
	}
	else
	{
		cout << "str1小于str2" << endl;
	}
}

int main()
{
	test01();
	return 0;
}

string字符存取

cpp 复制代码
//author:至尊宝
//time:2024.5.5
#include<iostream>
#include<cstring>
using namespace std;
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();
	return 0;
}

string插入与删除

cpp 复制代码
//author:至尊宝
//time:2024.5.5
#include<iostream>
#include<cstring>
using namespace std;
void test01()
{
	string str = "hello";

	//插入
	str.insert(1, "111");
	cout << "str = " << str << endl;
	//删除
	str.erase(1, 3);
	cout << "str = " << str << endl;
}

int main()
{
	test01();
	return 0;
}

string字串

cpp 复制代码
//author:至尊宝
//time:2024.5.5
#include<iostream>
#include<cstring>
using namespace std;
void test01()
{
	string str = "abcdef";
	string str2 = str.substr(1, 3);
	cout << str2 << endl;
}
void test02()
{
	string email = "hello@sina.com";
	//从邮件地址中获取用户名信息
	int pos = email.find("@");

	string name = email.substr(0, pos);
	cout << name << endl;
}

int main()
{
	test02();
	return 0;
}
相关推荐
娅娅梨32 分钟前
C++ 错题本--not found for architecture x86_64 问题
开发语言·c++
兵哥工控36 分钟前
MFC工控项目实例二十九主对话框调用子对话框设定参数值
c++·mfc
我爱工作&工作love我44 分钟前
1435:【例题3】曲线 一本通 代替三分
c++·算法
娃娃丢没有坏心思1 小时前
C++20 概念与约束(2)—— 初识概念与约束
c语言·c++·现代c++
lexusv8ls600h1 小时前
探索 C++20:C++ 的新纪元
c++·c++20
lexusv8ls600h1 小时前
C++20 中最优雅的那个小特性 - Ranges
c++·c++20
白-胖-子1 小时前
【蓝桥等考C++真题】蓝桥杯等级考试C++组第13级L13真题原题(含答案)-统计数字
开发语言·c++·算法·蓝桥杯·等考·13级
好睡凯1 小时前
c++写一个死锁并且自己解锁
开发语言·c++·算法
依旧阳光的老码农2 小时前
标准C++ 字符串
开发语言·c++
白-胖-子3 小时前
【蓝桥等考C++真题】蓝桥杯等级考试C++组第13级L13真题原题(含答案)-成绩排序
c++·算法·蓝桥杯·真题·蓝桥等考