【C++初阶】string类(二):常用接口全解析

🎬 博主名称键盘敲碎了雾霭
🔥 个人专栏 : 《C语言》《数据结构》 《C++》

⛺️指尖敲代码,雾霭皆可破


文章目录

  • 一、Modifiers(修正)
    • [1.1 push_back](#1.1 push_back)
    • [1.2 append](#1.2 append)
    • [1.3 +=(最常用的)](#1.3 +=(最常用的))
    • [1.4 insert](#1.4 insert)
    • [1.5 erase](#1.5 erase)
    • [1.6 replace](#1.6 replace)
  • 二、字符操作
    • [2.1 find](#2.1 find)
    • [2.2 c_str](#2.2 c_str)
    • [2.3 substr](#2.3 substr)
    • [2.4 rfind](#2.4 rfind)
    • [2.5 find_first_of](#2.5 find_first_of)
    • [2.6 find_last_of](#2.6 find_last_of)
  • 三、非成员函数
    • [3.1 operator+](#3.1 operator+)
    • [3.2 getline()](#3.2 getline())
    • [3.3 流插入和流提取](#3.3 流插入和流提取)
  • 文章结语

受篇幅限制,部分 string 相关接口放在了上一篇文章中,还没看过的朋友可以先点击下方链接快速回顾一下
https://blog.csdn.net/2401_89538720/article/details/159590175?fromshare=blogdetail&sharetype=blogdetail&sharerId=159590175&sharerefer=PC&sharesource=2401_89538720&sharefrom=from_link

一、Modifiers(修正)

1.1 push_back

cpp 复制代码
	string s1("hellow world");
	s1.push_back('x');
	cout << s1 << endl;

1.2 append

cpp 复制代码
	string s1("hellow world");
	s1.append("xxx");
	cout << s1 << endl;

1.3 +=(最常用的)

空间不够会自动扩容

cpp 复制代码
	string s1("hellow world");
	s1 += 'x';
	s1 += "jjjjj";
	cout << s1 << endl;

1.4 insert

cpp 复制代码
	string s1("hellow world");
	//插入一个字符
	s1.insert(0, "p");
	s1.insert(0, 1, 'x');
	s1.insert(s1.begin(), 't');
	cout << s1 << endl;

1.5 erase

cpp 复制代码
	string s1("hellow world");
	//s1.erase(0, 1);
	//s1.erase(s1.begin());
	//s1.erase(--s1.end());
	//s1.erase(s1.size()-1,1);
	s1.erase(0);//默认缺省值是npos,相当于全删
	cout << s1 << endl;

1.6 replace

伴随着增缩容

cpp 复制代码
	string s1("hellow world");
	//s1.replace(6, 4,"%%");
	s1.replace(6, 1,"%%");
	cout << s1 << endl;

二、字符操作

2.1 find

修改空格(方法一)

cpp 复制代码
	string s1("hellow wo rd ha ha ");
	size_t pos = s1.find(' ');
	while (pos != string::npos)
	{
		s1.replace(pos, 1, "%%");
		pos = s1.find(' ', pos + 2);
	}
	cout << s1 << endl;

追求效率(因为replace会反复移动数组,效率低下)

cpp 复制代码
	string s1("hellow wo rd ha ha ");
	string s2;
	for (auto ch : s1)
	{
		if (ch != ' ')
		{
			s2 += ch;
		}
		else
		{
			s2 += "%%";
		}
	}
	s1.swap(s2);
	cout << s2 << endl;

2.2 c_str

主要用来兼容C语言的,返回底层指针

cpp 复制代码
	cout << s1.c_str();
cpp 复制代码
	string s1;
	cin >> s1;
	FILE* pf = fopen(s1.c_str(), "r");
	char ch = fgetc(pf);
	while (ch != EOF)
	{
		cout << ch;
		ch = fgetc(pf);
	}
	fclose(pf);

2.3 substr

会自己构造string对象

cpp 复制代码
	string s1("haha.cpp.zip");
   size_t pos = s1.find(".");
   string tmp =s1.substr(pos);
   cout << tmp << endl;

2.4 rfind

cpp 复制代码
	string s1("haha.cpp.zip");
	size_t pos = s1.rfind(".");
	string tmp =s1.substr(pos);
	cout << tmp << endl;

2.5 find_first_of

cpp 复制代码
	string s1("addefrecvrgei");
	size_t pos = s1.find_first_of("aei");
	while (pos != string::npos)
	{
		s1.replace(pos, 1, "*");
		pos = s1.find_first_of("aei", pos + 1);
	}
	cout << s1 << endl;

2.6 find_last_of

cpp 复制代码
void fuc(string s1)
{
	size_t pos = s1.find_last_of("\\/");
	cout <<"路径:" << s1.substr(0,pos) << endl;
	cout <<"文件名:" << s1.substr(pos+1) << endl;
}
int main()
{
	string s1("C:\\Users\\hlf13\\Documents\\Visual Studio 2022");
	string s2("user/id/test.cpp");
	fuc(s1);
	fuc(s2);
}

三、非成员函数

3.1 operator+

cpp 复制代码
	string s1 ("haha");
	cout << s1+"he";
	cout << s1+'c';
	cout << "abb"+s1;

3.2 getline()

cpp 复制代码
int main()
{
    string s1;
    getline(cin,s1);
    size_t pos =s1.rfind(' ');
    string s2=s1.substr(pos+1);
    cout<<s2.size()<<endl;
}

3.3 流插入和流提取

文章结语

感谢你读到这里~我是「键盘敲碎了雾霭」,愿这篇文字帮你敲开了技术里的小迷雾 💻

如果内容对你有一点点帮助,不妨给个暖心三连吧👇

👍 点赞 | ❤️ 收藏 | ⭐ 关注

(听说三连的小伙伴,代码一次编译过,bug绕着走~)

你的支持,就是我继续敲碎技术雾霭的最大动力 🚀

🐶 小彩蛋:

复制代码
      /^ ^\
     / 0 0 \
     V\ Y /V
      / - \
    /    |
   V__) ||

摸一摸毛茸茸的小狗,赶走所有疲惫和bug~我们下篇见 ✨

相关推荐
前端郭德纲2 小时前
JavaScript原生开发与鸿蒙原生开发对比
开发语言·javascript·harmonyos
stolentime2 小时前
树套树+标记永久化:[POI 2006] TET-Tetris 3D&&SPOJ1741 TETRIS3D - Tetris 3D题解
c++·算法·线段树·树套树·标记永久化
csbysj20202 小时前
JSP 指令
开发语言
LSL666_2 小时前
JVM面试题——垃圾回收GC
java·开发语言·jvm
cch89182 小时前
PHP vs 易语言:Web开发与桌面编程大对决
开发语言·前端·php
江公望2 小时前
GNU C语句表达式,10分钟讲清楚
c语言·开发语言·c++
初中就开始混世的大魔王2 小时前
3.2 DDS 层-Domain
开发语言·c++·中间件
sdm0704272 小时前
Linux-库制作与原理
linux·c++·操作系统