【C++】字符串(string)的使用_c++ string 子串-程序员宅基地

技术标签: C++  c++  容器  

一、string 简介

C++ 中,std::string 是用于处理字符串的标准库类。它提供了一系列成员函数和操作符,使得字符串的操作更加方便和灵活。

string与char*的区别:

  • char* 是一个指针
  • string本质上是一个类,类的内部封装了char*,即string是一个char*型的容器
  • string管理char*所分配的内存,不用担心复制越界和取值越界等

二、string 构造函数

构造函数原型 解释
1 string() 创建一个空的字符串
2 string(const char* c) 使用字符串c初始化
3 string(const string& str) 使用string对象初始化
4 string(int n, char c) 使用n个字符c初始化

示例:

#include <iostream>
#include <string>
using namespace std;

void test01()
{
    
	string s1;				//创建空字符串,调用无参构造函数
	string s2("hello C++"); //把const char*转换成了string
	string s3(s2);          //调用拷贝构造函数,使用s2初始化s3
	string s4(10, 'c');

	cout << "s1 = " << s1 << endl;
	cout << "s2 = " << s2 << endl;
	cout << "s3 = " << s3 << endl;
	cout << "s4 = " << s4 << endl;
}
int main() 
{
    
	test01();
	system("pause");
	return 0;
}  
//result
s1 =
s2 = hello C++
s3 = hello C++
s4 = cccccccccc

三、string 字符串拼接

函数原型: += 、append 解释
1 string& operator+=(const char* str) 重载+=操作符
2 string& operator+=(const char c) 重载+=操作符
3 string& operator+=(const string& str) 重载+=操作符
4 string& append(const char *s) 把字符串s连接到当前字符串结尾
5 string& append(const char *s, int n) 把字符串s的前n个字符连接到当前字符串结尾
6 string& append(const string &s) 同3
7 string& append(const string &s, int p, int n) 将字符串s下标p开始的n个字符连接到当前字符串结尾

示例:

#include <iostream>
#include <string>
using namespace std;

void test01()
{
    
    string s1 = "AB";
	s1 += "CD";         //第1种拼接方法:+=char*字符串
	cout << "s1 = " << s1 << endl;
    
    s1 += 'e';			//第2种拼接方法:+=字符
    cout << "s1 = " << s1 << endl;
    
    string s0 = "FG";
    s1 += s0;			//第3种拼接方法;+=string字符串
    cout << "s1 = " << s1 << endl;
	
    string s2 = "AB";
	s2.append("CD");	//第4种拼接方法;append("");
    cout << "s2 = " << s2 << endl;
    
    s2.append("EFGH",3); //第5种拼接方法;append("",n);
    cout << "s2 = " << s2 << endl;
    
    string s3 = "HIJ";
    s2.append(s3); 		//第6种拼接方法;append(string);
    cout << "s2 = " << s2 << endl;
    
    string s4 = "KLMNOPQ";
    s2.append(s4,0,5); 		//第7种拼接方法;append(string,p,n);
    cout << "s2 = " << s2 << endl;
}
int main() 
{
    
	test01();
	system("pause");
	return 0;
}
//result
s1 = ABCD
s1 = ABCDe
s1 = ABCDeFG
s2 = ABCD
s2 = ABCDEFG
s2 = ABCDEFGHIJ
s2 = ABCDEFGHIJKLMNO

四、string 查找与替换

函数原型:find、rfind、replace 解释
1 int find(const string& s, int p) 从p开始查找s第一次出现位置
2 int find(const char* s, int p) 同1
3 int find(const char* s, int p, int n) 从p位置查找s的前n个字符第一次位置
4 int find(const char c, int p) 同1
5 int rfind(const string& s, int p) 从p开始查找s最后一次位置
6 int rfind(const char* s, int p) 同5
7 int rfind(const char* s, int p, int n) 从p查找s的前n个字符最后一次位置
8 int rfind(const char c, int p) 查找字符c最后一次出现位置
9 string& replace(int p, int n, const string& s) 替换p开始n个字符为字符串s
10 string& replace(int p, int n,const char* s) 同9

注意:

  • find查找是从左向后,rfind从右向左
  • findrfind找到指定字符串时,返回查找的第一个字符的位置下标;未找到则返回-1
  • replace在替换时,要指定起始位置p,替换字符数量n,替换后的字符串s

示例:

#include <iostream>
#include <string>
using namespace std;

void test01()
	{
    
	//查找
	string s1 = "ABCDEFGHIGK";
	int p = s1.find("EF");
	if (p == -1)
	{
    
		cout << "not find!" << endl;
	}
	else
	{
    
		cout << "find p = " << p << endl;
	}
	p = s1.rfind("EF");
	if (p == -1)
	{
    
		cout << "not find!" << endl;
	}
	else
	{
    
		cout << "rfind p = " << p << endl;
	}
}

void test02()
{
    
	//替换
	string s1 = "ABCDEF";
	s1.replace(1, 3, "0000");
	cout << "s1 = " << s1 << endl;
}

int main()
{
    
	test01();
	test02();
	system("pause");
	return 0;
}
//result
find p = 4
rfind p = 4
s1 = A0000EF

五、string 字符串比较

函数原型:compare 解释
1 int compare(const string &s) 与字符串s比较
2 int compare(const char *s) 同1

注意:

  • 字符串比较是按字符的ASCII码进行对比(= 返回 0) (> 返回 1) (< 返回 -1)
  • 字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义不大

示例:

#include <iostream>
#include <string>
using namespace std;

void test01()
{
    
	string s1 = "aaaaa";
	string s2 = "AAAAA";
	int ret = s1.compare(s2); 
	if (ret == 0) 
	{
    
		cout << "s1 等于 s2" << endl;
	}
	else if (ret > 0)
	{
    
		cout << "s1 大于 s2" << endl;
	}
	else
	{
    
		cout << "s1 小于 s2" << endl;
	}
}

int main()
{
    
	test01();
	system("pause");
	return 0;
}
//result
s1 大于 s2

六、string 字符获取、插入、删除

函数原型:[]、at、insert、erase 解释
1 char& operator[](int n) 重载[]操作符
2 char& at(int n) 通过.at方法获取字符
3 string& insert(int p, const char* s) 在下标p处插入字符串
4 string& insert(int p, const string& str) 同3
5 string& insert(int p, int n, char c) 在指定位置插入n个字符c
6 string& erase(int p, int n) 删除从P开始的n个字符

示例:

#include <iostream>
#include <string>
using namespace std;

void test01()
{
    
    string s = "ABCDEFG";
    cout << "s[1] = " << s[1] << endl;
    //修改s的第二个字符
    s.at(1) = 'X';
    cout << "s.at(1)" << s.at(1) << endl;

    //插入字符串
    s.insert(1, "555");
    cout << "s.insert = " << s << endl;

    //删除字符串
    s.erase(1, 3);
    cout << "s.erase = " << s << endl;
}

int main()
{
    
    test01();
    system("pause");
    return 0;
}
//result
s[1] = B
s.at(1)X
s.insert = A555XCDEFG
s.erase = AXCDEFG

七、string 子串

函数原型:substr 解释
1 string substr(int p = 0, int n) 返回由p开始的n个字符组成的字符串

示例:

#include <iostream>
#include <string>
using namespace std;

void test01()
{
    
    string s1 = "ABCDEFG";
    //截取子字符串
    string s2 = s1.substr(1,4);
    cout << "s2 = " << s2 << endl;
}

int main()
{
    
    test01();
    system("pause");
    return 0;
}
//result
s2 = BCDE

如果这篇文章对你有所帮助,渴望获得你的一个点赞!

在这里插入图片描述

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/AAADiao/article/details/130755661

智能推荐

物联网开发技术栈_物联网技术java技术栈-程序员宅基地

文章浏览阅读2.2k次,点赞2次,收藏10次。物联网开发技术栈 内容简介作为互联网技术的进化,物联网开发并非孤立的技术栈,而是向上承接了互联网,向下统领了嵌入式硬件开发的一个承上启下的全栈开发技术。虽然我们并不能预测物联网技术栈最终的样子:统一的开发语言是 JavaScript 还是 Python 亦或者其他编程语言;HTTP、WebSockets、MQTT、CoAP 等协议谁会是最后的赢家,并且随着物联网的不断进化,甚至我们..._物联网技术java技术栈

《Git学习笔记:Git入门 & 常用命令》-程序员宅基地

文章浏览阅读674次,点赞10次,收藏11次。Git是一个分布式版本控制工具,通常用来对软件开发过程中的源代码文件进行管理,通过Git仓库来存储和管理这些文件,Git仓库分为两种:指的是存储在各个开发人员自己本机电脑上的Git仓库指的是远程服务器上的Git仓库commit:提交,将本地文件和版本信息保存到本地仓库push:推送(上传),将本地仓库文件和版本信息上传到远程仓库pull:拉取(下载),将远程仓库文件和版本信息下载到本地仓库。

CPU热点分析——pprof (gperftools)使用_gperftools pprof-程序员宅基地

文章浏览阅读4.6k次。pprof (gperftools)使用谷歌的工具集,可查看CPU采样结果。pprof (google-perftool),用于来分析程序,必须保证程序能正常退出。使用步骤:1.准备工具,先安装工具包libunwind-1.1.tar.gzgperftools-2.1.tar.gz解压后 configure到系统默认路径即可,之后直接-lprofiler 2.再安装图形工具sudo yum ins..._gperftools pprof

JavaScript BOM-程序员宅基地

文章浏览阅读118次。JavaScript BOM:Navigator、History、Location

MongoDB数据库 —— 图形化工具_mongodb数据库图形化工具-程序员宅基地

文章浏览阅读6.2k次,点赞16次,收藏66次。在前面通过使用MongoDB在命令窗口操作数据库,而MySQL数据库也同样可以在命令窗口使用sql语句操作数据库,在安装数据库的时候提到可以安装这个图形化工具的,为了节省安装时间和卡顿选择后续安装MongoDB图形化工具,在MySQL数据中同样也有这个MySQL workbench 图形化工具可以选择进行安装;那么本篇就来安装MongoDB的图形化工具 — MongoDBCompass。_mongodb数据库图形化工具

ChatGPT带给智慧城市的启示——未来城市演进路径的探讨-程序员宅基地

文章浏览阅读1.4k次,点赞13次,收藏10次。未来城市的大模型包括城市总体规划、城市交通运输管理、城市公共安全和应急管理、经济发展和产业园区发展、社区发展、资源承载调控、污染调控、社会资源优化调控、基础设施调控、人口研究等模型。其对城市要素、关键指标、函数、流程、模型、平台、技术、资金、人才、市场、自然环境等内外部因素进行仿真建模。采用物联网、云计算、大数据、数字孪生和人工智能等技术来获取地、物、人、组织、环境、社会、经济、业务逻辑和运营规律等相关数据。、物联网、大数据、云计算、数字孪生、元宇宙、可穿戴生理传感器、分布式新能源等各类新技术。

随便推点

从零开始开发Shopify主题:(4)调用自定义配置_shopify自定义主题-程序员宅基地

文章浏览阅读3.6k次。在上一篇文章中,我们知道了如何使用配置文件自定义主题,以允许商店所有者自己更改Shopify主题。 如上所述,这些设置会在用户单击管理面板的在线商店>主题部分中的自定义主题按钮时显示,并在主题开发文件的config / settings_schema.json文件中定义。在这篇文章中,我们将了解如何访问这些设置并在开发主题时调用它们。调用配置要调用模板中的配置信息,需要使用li..._shopify自定义主题

git本地分支与远程分支关联及遇到的问题解决方案_本地分支 '(no branch)' (远程分支 = '(no branch)') 是无效的。引用名-程序员宅基地

文章浏览阅读2.6k次。1.查看本地分支git branch绿色表示当前分支#######################################################2.查看远程分支git branch -a#######################################################3.切换分支git checkout branch_name..._本地分支 '(no branch)' (远程分支 = '(no branch)') 是无效的。引用名称必须遵循

java连接mysql出现The server time zone value '�й���׼ʱ��' is unrecognized的解决方法_java.lang.runtimeexception: the server time zone v-程序员宅基地

文章浏览阅读2.2w次,点赞18次,收藏28次。java连接mysql出现The server time zone value '�й���׼ʱ��' is unrecognized的解决方法在Idea中连接数据库是抛出The server time zone value ‘�й���׼ʱ��’ is unrecogni错误 原因是因为使用了Mysql Connector/J 6.x以上的版本,然后就报了时区的错误。解决办法在配置url中添..._java.lang.runtimeexception: the server time zone value '嚙請對蕭嚙踝蕭

鸿蒙原生应用元服务实战-Serverless华为账户认证登录需尽快适配-程序员宅基地

文章浏览阅读671次,点赞11次,收藏9次。并且在这个固定的serverless服务中去增加这个,应该不是应用元服务开发者有这个加入权限的,应该是要统一解决,类似实现和手机注册验证一样的,直接可以使用或者少量代码配置即可使用。另外就是如果是新的元服务应用,使用的serverless,如果不支持华为账户功能,就没法上架,这个也是比较麻烦的,前面已经使用serverless开发基本完成或者已经完成的,得用其他方式去实现才行吧。对于已经上架的应用和元服务、升级也没法进行。3月1日的时间是快到了。

使用XAMPP可视化管理Mysql,使用JDBC访问数据库执行插入、查询、删除等操作_xammp进入可视化界面-程序员宅基地

文章浏览阅读5.7k次,点赞2次,收藏8次。准备工作:安装XAMPP,登陆apache,mysql,并通过phpadmin来创建数据库,新建一个表,插入一些数据:http://localhost/phpmyadmin,最好设置密码,不然后面连接数据库的时候可能会无法访问设置密码方式:修改密码--->一定要使用生成的密码来登陆,包括后面的数据库url也是。我简历的数据如下:这时候就可以在eclipse中编程开发_xammp进入可视化界面

(转) spring 的jar各包作用-程序员宅基地

文章浏览阅读119次。转自:http://blog.csdn.net/cailiang517502214/article/details/4797642spring.jar是包含有完整发布的单个jar包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到spring-mock.jar来进行辅助测试,正式应用系统中是用不得这些类的。...

推荐文章

热门文章

相关标签