【C++】string类(二)相关接口介绍及其使用

文章目录

上一篇文章链接:string(一)

(该篇为(一)的续作)

一、string相关接口


1、append

cpp 复制代码
void test04()
{
	string s("hello world");
	s.append("aaa");
	cout << s << endl;
}

2、push_back

对字符串尾插一个字符

cpp 复制代码
void test05()
{
	string s("hello world");
	s.push_back('A');
	cout << s << endl;
}

3、+=

+= 也是用来拼接字符/字符串的,相较以上两种接口,+=更常用。

cpp 复制代码
void test06()
{
	string s("hello world");
	s += 'a';
	s += "bbb";
	cout << s << endl;
}

4、insert

插入

最常用的是第一个,在pos位置插入字符串。

cpp 复制代码
void test07()
{
	string s("hello world");
	s.insert(0, "w");
	cout << s << endl;
}

5、erase

删除元素

cpp 复制代码
void test08()
{
	string s("hello world");
	s.erase(0, 1);//在第0个位置删除1个元素
	cout << s << endl;
}
cpp 复制代码
void test08()
{

	string s("hello world");
	//头删
	s.erase(0, 1);//在第0个位置删除1个元素
	//尾删
	// s.erase(s.size() - 1, 1);
	s.erase(--s.end());//迭代器
	cout << s << endl;
}

6、replace

替换

cpp 复制代码
void test09()
{
	string s("hello world");
	s.replace(0, 2, "a");//从第0个位置开始的2个字符替换为"a"
	cout << s << endl;
}

7、find

查找子字符串str在该字符串中的位置,查找成功返回该子串的首字符位置,失败返回npos

cpp 复制代码
void test11()
{
	string s("hello world");
	cout << s.find(" ");//输出结果为:5
}

练习

练习:

将字符串中的空格全部替换为"%%"

(1)方法1:使用find、replace

cpp 复制代码
void test10()
{
	string s("hello world  aaa bbb ccc  f");
	int pos = s.find(' ');
	while (pos != string::npos)
	{
		s.replace(pos, 1, "%%");
		pos = s.find(' ', pos + 2);

	}
	cout << s << endl;
}

(2)方法2:遍历替换

cpp 复制代码
void test10()
{
	string s("hello world  aaa bbb ccc  f");
	string tmp;
	for (auto ch : s)
	{
		if (ch == ' ')
			tmp += "%%";
		else
			tmp += ch;
	}
	s = tmp;
	cout << s << endl;
}

8、rfind

和find作用相同,只不过是倒着查找。

cpp 复制代码
void test11()
{
	string s("hello world ddd");
	cout << s.rfind(" ");//输出结果为:11
}

9、find_first_of

与字符串str中任一字符进行匹配。

例如:将字符串s中含有a/b/c/d的字符全部修改为*

cpp 复制代码
void test12()
{
	string s("hello world  abfg cdmn");
	int pos = s.find_first_of("abcd");
	while (pos != string::npos)
	{
		s.replace(pos, 1, "*");
		pos = s.find_first_of("abcd", pos + 1);

	}
	cout << s << endl;
}

10、find_last_of

与字符串str中任一字符进行匹配,但是倒着进行查找匹配。

11、find_first_not_of

排除,查找字符串中非str中任一字符的下标。

例如:将字符串s中非a/b/c/d的字符全部修改为*

cpp 复制代码
void test12()
{
	string s("hello world  abfg cdmn");
	int pos = s.find_first_not_of("abcd");
	while (pos != string::npos)
	{
		s.replace(pos, 1, "*");
		pos = s.find_first_not_of("abcd", pos + 1);

	}
	cout << s << endl;
}

12、find_last_not_of

查找字符串中非str中任一字符的下标,但是倒着进行查找匹配。

13、substr

拷贝该字符串中第pos位置,长度为len的子串。

cpp 复制代码
void test13()
{
	string s("hello world");
	
	cout << s.substr(6) << endl;
}

二、谢谢观看!

相关推荐
weixin_4723394621 分钟前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
枯萎穿心攻击1 小时前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎
Eiceblue2 小时前
【免费.NET方案】CSV到PDF与DataTable的快速转换
开发语言·pdf·c#·.net
tan180°3 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
m0_555762903 小时前
Matlab 频谱分析 (Spectral Analysis)
开发语言·matlab
哲科软件4 小时前
跨平台开发的抉择:Flutter vs 原生安卓(Kotlin)的优劣对比与选型建议
android·flutter·kotlin
浪裡遊4 小时前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
彭祥.4 小时前
Jetson边缘计算主板:Ubuntu 环境配置 CUDA 与 cudNN 推理环境 + OpenCV 与 C++ 进行目标分类
c++·opencv·分类
lzb_kkk5 小时前
【C++】C++四种类型转换操作符详解
开发语言·c++·windows·1024程序员节
好开心啊没烦恼5 小时前
Python 数据分析:numpy,说人话,说说数组维度。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy