STL——string容器

本文深入探讨了C++中的字符串操作,包括如何进行字符串赋值(如使用assign函数)、字符串拼接(使用+=和append方法)、查找与替换(find和replace函数)、字符串比较(compare方法)以及字符存取(通过[]和at方法)。这些基础知识对于C++程序员来说至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

string容器赋值操作

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

//string赋值操作

void test1()
{
	string str1;
	str1 = "石头人";
	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 world");//通过内置函数assign对其进行赋值
	cout << "str4" << str4 << endl;

	string str5;
	str5.assign("hello world", 5);//将字符串的前n个字符对其赋值
	cout << "str5" << str5 << endl;

	string str6;
	str6.assign(str3);//用assign将str3赋值给str6
	cout << "str6" << str6 << endl;

	string str7;
	str7.assign(10, 'w');//将10个w赋值给str7
	cout << "str7" << str7 << endl;
}

string字符串拼接:在字符串末位拼接字符串

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

//string字符串拼接

void test1()
{
	string str1 = "我";
	str1 += "爱玩游戏";//通过+=实现str1拼接后续字符串
	cout << "str1=" << str1 << endl;

	str1 += ':';//仅拼接字符
	string str2 = "ABCDE";
	str1 += str2;//通过+=将str2拼接到str1后面

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

	str3.append("game do", 4);//将字符串的前4位拼接到str3后面
	cout << "str3=" << str3 << endl;
	str3.append(str2);//将str2拼接到str3后面
	cout << "str3=" << str3 << endl;
	str3.append(str2, 2);//截取str2的前2位拼接到str3后面
	cout << "str3=" << str3 << endl;
	str3.append(str2, 3,5);//截取str2的第3位到第5位拼接到str3后面
	cout << "str3=" << str3 << endl;
}

string容器——字符串查找和替换

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

//字符串查找和替换

//1.查找
void test1()
{
	string str1="abcdef";

	int pos = str1.find("de");//查找结果为位置下标,下标从0开始计算
	//当需要查找的字符串没有时,会返回-1值,意味着没找到
	cout << "pos" << pos << endl;

	//rfind和find的区别
	//rfind从右往左查找,find从左往右查找,当查找到第一个符合要求的字符串就停止查找
	pos=str1.rfind("de");
	cout << "pos" << pos << endl;
}

void test2()
{
	string str1 = "abcdefgh";
	str1.replace(1, 3, "SSSS");// 从1号位到3号位,替换为SSSS
}

string字符串比较

当字符串相等时返回0;不相等时返回1或-1

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

//字符串比较
//字符串比较时,编译器从前往后逐个比较字符的ASCII码大小
//完全相等时输出0;第一个不相等的字符位比较出大小后直接输出大小比较结果,不再往后检查
void test1()
{
	string str1 = "helwo";
	string str2 = "hello";
	if (str1.compare(str2) == 0)//两个字符串相等
	{
		cout << "=" << endl;
	}
	else if (str1.compare(str2) == 1)//str1>str2
	{
		cout << ">" << endl;
	}
	else if(str1.compare(str2) == -1)//str1<str2
	{
		cout << "<" << endl;
	}
}

string 字符存取

//string字符存取
void test1()
{
	string str = "hello";
	//1.通过[]访问单个字符
	for (int i = 0; i < str.size(); i++)
	{
		cout << str[i] << " " << endl;
	}
	cout << endl;

	//2.通过at方式访问单个字符
	for (int i = 0; i < str.size(); i++)
	{
		cout << str.at(i)<< " " << endl;
	}
	cout << endl;

	//修改单个字符
	str[0] = 'x';
	cout << "str" << str << endl;

	str.at(1) = 'x';
}

string子串

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

//string求子串

void test1()
{
	string str = "abcdef";
	string substr = str.substr(1, 3);//从号位开始取出3位字符赋值给substr

	cout << "substr=" << substr << endl;
}

void test2()
{
	string email = "hello@sina.com";
	//从邮件地址中获取用户名信息
	int pos=email.find("@");//获取@的位置

	string username = email.substr(0, pos);//输出为0号位后面的pos个字符串

	cout << username << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值