C++中string容器的字符串操作

目录

1.c_str() 返回C常量字符串

2.date() 返回C常量字符串

3.substr() 构造子串

4.find() 正向查找(查找失败返回npos)

5.rfind() 逆向查找(查找失败返回npos)

6.find_first_of() 正向查找匹配的字符

7.find_last_of() 逆向查找匹配的字符

8.find_first_not_of() 正向查找不匹配的字符

9.find_last_not_of() 逆向查找不匹配的字符

10.compare() 比较字符串


1.c_str() 返回C常量字符串

const char* c_str() const 

string s("123");
const char* p = s.c_str();
cout << p << endl;//123

2.date() 返回C常量字符串

const char* data() const

string s("12345");
const char* p = s.data();
cout << p << endl;//12345

3.substr() 构造子串

string substr(size_t pos = 0, size_t len = npos) const 

返回pos位置开始的len个字符组成的字符串

string s1 = "12345";
string s2 = s1.substr(2, 3);
cout << s2 << endl;//345

4.find() 正向查找(查找失败返回npos)

string (1)
size_t find (const string& str, size_t pos = 0) const;
c-string (2)
size_t find (const char* s, size_t pos = 0) const;
buffer (3)
size_t find (const char* s, size_t pos, size_t n) const;
character (4)
size_t find (char c, size_t pos = 0) const;

1.size_t find(const string& str, size_t  pos = 0) const 

从pos位置开始查找匹配的对象str,返回查找到的位置

2.size_t find(const char* s, size_t pos = 0) const 

从pos位置开始查找匹配的字符串s,返回查找到的位置

3.size_t find(const char* s, size_t pos, size_t n)

从pos位置查找匹配字符串s的前n个字符,返回查找到的位置

4.size_t find(char c, size_t pos = 0)

从pos位置开始查找字符c,返回查找到的位置

string s1("There are two needles in this haystack with needles.");
string s2("needle");
size_t found = s1.find(s2, 0);
if (found != string::npos)
{cout << "first 'needle' found at " << found << endl;//first 'needle' found at 14
}
else
{cout << "'needle' no found" << endl;
}found = s1.find("needle are small", found + 1, 6);
if (found != string::npos)
{cout << "second 'needle' found at " << found << endl;//second 'needle' found at 44
}
else
{cout << "'needle' no found" << endl;
}found = s1.find("haystack", 0);
if (found != string::npos)
{cout << "'haystack' found at " << found << endl;//'haystack' found at 30
}
else
{cout << "'haystack' no found" << endl;
}found = s1.find('.', 0);
if (found != string::npos)
{cout << "'.' found at " << found << endl;//'.' found at 30
}
else
{cout << "'.' no found" << endl;
}

5.rfind() 逆向查找(查找失败返回npos)

string (1)
size_t rfind (const string& str, size_t pos = npos) const;
c-string (2)
size_t rfind (const char* s, size_t pos = npos) const;
buffer (3)
size_t rfind (const char* s, size_t pos, size_t n) const;
character (4)
size_t rfind (char c, size_t pos = npos) const;

1.size_t rfind(const string& str, size_t pos =npos) const

从pos位置逆向开始查找匹配的对象str,返回查找到的位置

2.size_t rfind(const char* s, size_t pos =npos) const

从pos位置逆向开始查找匹配的字符串s,返回查找到的位置

3.size_t rfind(const char* s, size_t pos, size_t n) const

从pos位置逆向查找匹配字符串s的前n个字符,返回查找到的位置

4.size_t rfind(char c, size_t pos =npos) const

从pos位置逆向开始查找字符c,返回查找到的位置

string str("The sixth sick sheik's sixth sheep's sick.");
string key("sixth");
size_t found = str.rfind(key);
if (found != string::npos)cout << "'sixth' found at " << found << endl;//'sixth' found at 23

6.find_first_of() 正向查找匹配的字符

正向查找首个与指定字符串中任一字符匹配的字符,查找失败返回npos

string (1)
size_t find_first_of (const string& str, size_t pos = 0) const;
c-string (2)
size_t find_first_of (const char* s, size_t pos = 0) const;
buffer (3)
size_t find_first_of (const char* s, size_t pos, size_t n) const;
character (4)
size_t find_first_of (char c, size_t pos = 0) const;

1.size_t find_first_of(const string& str, size_t pos = 0) const 

从pos位置开始查找对象str中首次出现的任一字符,查找失败返回npos

2.size_t find_first_of(const char* s, size_t pos = 0) const

从pos位置开始查找字符串s中出现的任一字符,查找失败返回npos

3.size_t find_first_of(const char* s, size_t pos, size_t n) const 

从pos位置开始查找字符串s的前n个字符串中出现的任一字符,查找失败返回npos

4.size_t find_first_of(char c, size_t pos =0 ) const 

从pos位置开始查找首次出现的字符c,查找失败返回npos

string s("Please, replace the vowels in this sentence by asterisks.");
size_t found = s.find_first_of("aoeiu", 0);
while (found != string::npos)
{s[found] = '*';found = s.find_first_of("aoeiu", found + 1);
}
cout << s << endl;//Pl**s*, r*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks.

7.find_last_of() 逆向查找匹配的字符

逆向查找首个与指定字符串中任一字符匹配的字符,查找失败返回npos

string (1)
size_t find_last_of (const string& str, size_t pos = npos) const;
c-string (2)
size_t find_last_of (const char* s, size_t pos = npos) const;
buffer (3)
size_t find_last_of (const char* s, size_t pos, size_t n) const;
character (4)
size_t find_last_of (char c, size_t pos = npos) const;

1.size_t find_last_of(const string& str, size_t pos = npos) const

从pos位置开始逆向查找首个与对象str中任一字符匹配的字符,查找失败返回npos

2.size_t find_last_of(const char* s, size_t pos = npos) const

ni从pos位置开始逆向查找首个与字符串s中任意字符匹配的字符,查找失败返回npos

3.size_t find_last_of(const char* s,size_t pos, size_t n) const

从pos位置开始逆向查找首个与字符串s的前n个字符中任意字符匹配的字符,查找失败返回npos

4.size_t find_last_of(char c, size_t pos =npos) const

ni从pos位置开始逆向查找首个与字符c匹配的字符,查找失败返回npos

void SplitFilename(const string& str)
{size_t found = str.find_last_of("/\\");cout << "path :" << str.substr(0, found) << endl;cout << "file :" << str.substr(found + 1) << endl;
}
void string_test()
{string s1("/usr/bin/man");string s2("c:\\windows\\winhelp.exe");SplitFilename(s1);SplitFilename(s2);}
int main()
{string_test();//path: / usr / bin//file : man//path : c:\windows//file : winhelp.exereturn 0;
}

 8.find_first_not_of() 正向查找不匹配的字符

正向查找首个与指定字符串中任一字符不匹配的字符,查找失败返回npos

string (1)
size_t find_first_not_of (const string& str, size_t pos = 0) const;
c-string (2)
size_t find_first_not_of (const char* s, size_t pos = 0) const;
buffer (3)
size_t find_first_not_of (const char* s, size_t pos, size_t n) const;
character (4)
size_t find_first_not_of (char c, size_t pos = 0) const;

1.size_t find_first_not_of(const string& str, size_t pos = 0) const 

从pos位置开始正向查找首个与对象str中字符不匹配的字符,查找失败返回npos

2.size_t find_first_not_of(const char* s, size_t pos = 0) const

从pos位置开始正向查找首个与字符串s中字符不匹配的字符,查找失败返回npos

3.size_t find_first_not_of(const char* s, size_t pos, size_t n) const

从pos位置开始正向查找首个与字符串s前n个字符中字符不匹配的字符,查找失败返回npos

4.size_t find_first_not_of(char c, size_t pos = 0) const

从pos位置开始正向查找首个与字符c不匹配的字符,查找失败返回npos

string str("look for non-alphabetic characters...");
size_t found = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz ");
if (found != string::npos)
{cout << "The first non-alphabetic character is " << str[found] << " at position " << found << endl;//The first non-alphabetic character is - at position 12
}

9.find_last_not_of() 逆向查找不匹配的字符

 逆向查找首个与指定字符串中任一字符不匹配的字符,查找失败返回npos

string (1)
size_t find_last_not_of (const string& str, size_t pos = npos) const;
c-string (2)
size_t find_last_not_of (const char* s, size_t pos = npos) const;
buffer (3)
size_t find_last_not_of (const char* s, size_t pos, size_t n) const;
character (4)
size_t find_last_not_of (char c, size_t pos = npos) const;

1.size_t find_last_not_of(const string& str, size_t pos = npos) const

从pos位置开始逆向查找首个与对象str中字符不匹配的字符,查找失败返回npos

2.size_t find_last_not_of(const char* s, size_t pos = npos) const

从pos位置开始逆向查找首个与字符串s中字符不匹配的字符,查找失败返回npos

3.size_t find_last_not_of(const char* s, size_t pos, size_t n) const

从pos位置开始逆向查找首个与字符串s前n个字符中字符不匹配的字符,查找失败返回npos

4.size_t find_last_not_of(char c, size_t pos = npos) const

从pos位置开始逆向查找首个与字符c不匹配的字符,查找失败返回npos

string str("Please, erase trailing white-spaces   \n");
string whitespaces(" \t\f\v\n\r");
size_t found = str.find_last_not_of(whitespaces);
if (found != string::npos)str.erase(found + 1);   //[Please, erase trailing white-spaces]
elsestr.clear();
cout << "[" << str << "]" << endl;

10.compare() 比较字符串

比较两个字符串的ASCII码值,字符串1大于字符串2返回大于0的数;字符串1等于字符串2返回0;字符串1小于字符串2返回小于0的数

string (1)
int compare (const string& str) const;
substrings (2)
int compare (size_t pos, size_t len, const string& str) const;
int compare (size_t pos, size_t len, const string& str,size_t subpos, size_t sublen) const;
c-string (3)
int compare (const char* s) const;
int compare (size_t pos, size_t len, const char* s) const;
buffer (4)
int compare (size_t pos, size_t len, const char* s, size_t n) const;

1.int compare(const string& str) const

比较调用对象和str对象的大小

2.int compare(size_t pos, size_t len, const string& str) const

比较调用对象pos位置开始的len个字符与对象str的大小

int compare(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const

比较调用对象pos位置开始的len个字符与对象str中subpos位置开始的sublen个字符的大小

3.int compare(const char* s) const

比较调用对象和字符串s的大小

int compare(size_t pos, size_t len, const char* s) const

比较调用对象从pos位置开始的len个字符与字符串s的大小

4.int compare(size_t pos, size_t len, const char* s, size_t n) const

比较调用对象从pos位置开始的len个字符与字符串s前n个字符的大小

//1.
string s1("abcdefg");
string s2("abcdfg");
if (s1.compare(s2) == 0)cout << s1 << " = " << s2 << endl;
else if (s1.compare(s2) > 0)cout << s1 << " > " << s2 << endl;
elsecout << s1 << " < " << s2 << endl; //abcdefg < abcdfg//2.1
string s1("abcdefg");
string s2("abcdfg");
if (s1.compare(1, 6, s2) == 0)cout << s1 << " = " << s2 << endl;
else if (s1.compare(1, 6, s2) > 0)cout << s1 << " > " << s2 << endl;//abcdefg > abcdfg
elsecout << s1 << " < " << s2 << endl; //2.2
string s1("abcdefg");
string s2("abcdfg");
if (s1.compare(1, 6, s2, 1, 5) == 0)cout << s1 << " = " << s2 << endl;
else if (s1.compare(1, 6, s2, 1, 5) > 0)cout << s1 << " > " << s2 << endl;
elsecout << s1 << " < " << s2 << endl;//abcdefg < abcdfg//3.1
string s("abcdefg");
char p[] = "abcdfg";
if (s.compare(p) == 0)cout << s << " = " << p << endl;
else if (s.compare(p) > 0)cout << s << " > " << p << endl;
elsecout << s << " < " << p << endl;//abcdefg < abcdfg//3.2
string s("abcdefg");
char p[] = "abcdfg";
if (s.compare(1, 5, p) == 0)cout << s << " = " << p << endl;
else if (s.compare(1, 5, p) > 0)cout << s << " > " << p << endl;//abcdefg > abcdfg
elsecout << s << " < " << p << endl;//4
string s("abcdefg");
char p[] = "abcdfg";
if (s.compare(1, 5, p, 5) == 0)cout << s << " = " << p << endl;
else if (s.compare(1, 5, p, 5) > 0)cout << s << " > " << p << endl;//abcdefg > abcdfg
elsecout << s << " < " << p << endl;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/789714.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

HTML面试题:get和post的区别

get和post都是HTTP中的两种请求方式 区别一&#xff0c;参数位置&#xff1a;GET请求把参数包含在URL中&#xff0c;POST将参数包含在请求体request body中。 区别二&#xff0c;回退&#xff1a;GET在浏览器回退时是无害的&#xff0c;而POST会再次提交请求。 区别三&#…

经典文献阅读之--LOG-LIO(高效局部几何信息估计的激光雷达惯性里程计)

0. 简介 局部几何信息即法线和点分布在基于激光雷达的同时定位与地图构建&#xff08;SLAM&#xff09;中是至关重要&#xff0c;因为它为数据关联提供了约束&#xff0c;进一步确定了优化方向&#xff0c;最终影响姿态的准确性。然而即使在使用KD树或体素图的辅助下&#xff…

【CANN训练营笔记】AscendCL图片分类应用(C++实现)

样例介绍 基于PyTorch框架的ResNet50模型&#xff0c;对*.jpg图片分类&#xff0c;输出各图片所属分类的编号、名称。 环境介绍 华为云AI1s CPU&#xff1a;Intel Xeon Gold 6278C CPU 2.60GHz 内存&#xff1a;8G NPU&#xff1a;Ascend 310 环境准备 下载驱动 wget ht…

git diff

1. 如何将库文件的变化生成到patch中 git diff --binary commit1 commit2 > test.patch 打patch&#xff1a; git apply test.patch 2. 如何消除trailing whitespace 问题 git diff --ignore-space-at-eol commit1 commit2 > test.patch 打patch&#xff1a; git ap…

在 Windows 中安装部署并启动连接 MongoDB 7.x(命令行方式启动、配置文件方式启动、将启动命令安装为系统服务实现开机自启)

MongoDB 的下载 下载地址&#xff1a;https://www.mongodb.com/try/download/community 这里需要对 MongoDB 的版本号说明一下&#xff1a; MongoDB 版本号的命名规则是 x.y.z&#xff0c;当其中的 y 是奇数时表示当前的版本为开发版&#xff0c;当其中的 y 是偶数时表示当前的…

非关系型数据库之Redis配置与优化

一、关系数据库与非关系型数据库 1.1关系型数据库 关系型数据库是一个结构化的数据库&#xff0c;创建在关系模型&#xff08;二维表格模型&#xff09;基础上一般面向于记录。SQL语句&#xff08;标准数据查询语言&#xff09;就是一种基于关系型数据库的语言&#xff0c;用…

Linux入侵排查

第2篇&#xff1a;Linux 入侵排查 0x00 前言 当企业发生黑客入侵、系统崩溃或其它影响业务正常运行的安全事件时&#xff0c;急需第一时间进行处理&#xff0c;使企业的网络信息系统在最短时间内恢复正常工作&#xff0c;进一步查找入侵来源&#xff0c;还原入侵事故过程&…

【C#】数字后缀及其作用 | Numeric Literal Suffixes and Their Usage in C#

C#中的数字字面量后缀及其作用 | Numeric Literal Suffixes and Their Usage in C# 在C#编程中,我们经常需要使用不同类型的数字,如整数、浮点数和高精度数字等。为了方便表示这些数字并明确其数据类型,C#提供了各种数字字面量后缀。本文将通过实例详细介绍这些后缀的作用和用…

【Python】【极简入门】12-元组

基本特性 元组就是一个逗号分隔的序列:t = ‘a’, ‘b’, ‘c’一般都用括号把元组括起来: t = (‘a’, ‘b’, ‘c’)创建单元素元组, 需要在末尾加上逗号: t1 = ‘a’, 不加逗号不构成元组:t2 = (‘a’)如果参数是一个序列 (字符串, 列表或者元组), 结果便会得到一个由元素…

首场直播,就在4月11日!

2024年的第一场直播&#xff0c;我们把目光聚焦到“大会员”。 这一次我们想聊聊&#xff0c;当大会员遇上泛零售企业&#xff0c;会产生怎样的“火花”。泛零售企业突破增长压力的机会在哪里&#xff1f;又有哪些挑战必须直面&#xff1f; 本次直播将结合泛零售企业“多业态、…

GEE问题——在使用sentienl数据云掩膜的时候发现出现中间连贯性的“条带”问题,如何解决?

简介 在使用sentienl+landsat数据掩膜的时候发现出现了中间连贯性的条带问题,如何解决?这里我们使用GEE出品的Landsat和sentinel数据的过程中,当我们进行云掩膜的时候出现了条带的问题。 问题 您注意到这个问题了吗? 我该如何消除它们(例如,在镶嵌前遮蔽瓦片最外层的 …

1379. 找出克隆二叉树中的相同节点

说在前面 &#x1f388;不知道大家对于算法的学习是一个怎样的心态呢&#xff1f;为了面试还是因为兴趣&#xff1f;不管是出于什么原因&#xff0c;算法学习需要持续保持。 题目描述 给你两棵二叉树&#xff0c;原始树 original 和克隆树 cloned&#xff0c;以及一个位于原始…

UniApp 应用发布到苹果商店指南

&#x1f680; 想要让你的 UniApp 应用在苹果商店亮相吗&#xff1f;别着急&#xff0c;让我来带你一步步完成这个重要的任务吧&#xff01;在这篇博客中&#xff0c;我将详细介绍如何将 UniApp 应用顺利发布到苹果商店&#xff0c;让你的应用跻身于苹果生态之中。 引言 &…

十八、Rust gRPC 多 proto 演示

十八、Rust gRPC 多 proto 演示 网上及各官方资料&#xff0c;基本是一个 proto 文件&#xff0c;而实际项目&#xff0c;大多是有层级结构的多 proto 文件形式&#xff0c;本篇文章 基于此诉求&#xff0c;构建一个使用多 proto 文件的 rust grpc 使用示例。 关于 grpc 的实现…

高德定位 SDK 到底提供了什么服务?

最近我被高德的销售烦到不行&#xff0c;说是我用了他们的 SDK&#xff0c;现在 SDK 要收费。 表达是很绕的&#xff0c;什么授权啦&#xff0c;什么企业认证风险啦&#xff0c;讲了一堆&#xff0c;还跟我开始搬出协议了。感觉高德的销售真够垃圾的&#xff0c;编个话术都不会…

Lua环境下载与配置

这里介绍如何下载已经编译好的Lua环境&#xff0c;如何配置Lua环境。 如希望自己从源码编译Lua环境&#xff0c;请自行搜索资料。 第一步&#xff1a;下载编译好的lua环境 打开下面链接&#xff0c;然后根据指引下载。 The Programming Language Luahttps://www.lua.org/hom…

windows下基于python语言的TTS开发

AI应用开发相关目录 本专栏包括AI应用开发相关内容分享,包括不限于AI算法部署实施细节、AI应用后端分析服务相关概念及开发技巧、AI应用后端应用服务相关概念及开发技巧、AI应用前端实现路径及开发技巧 适用于具备一定算法及Python使用基础的人群 AI应用开发流程概述Visual St…

算法打卡day33|动态规划篇01|动态规划理论基础| Leetcode 509. 斐波那契数、70. 爬楼梯、746. 使用最小花费爬楼梯

动态规划理论 定义 动态规划(Dynamic Programming&#xff0c;简称DP)&#xff0c;主要用于解决多阶段决策问题。它的核心思想是将一个复杂的多阶段问题转化为一系列相对简单的单阶段问题&#xff0c;然后逐一求解这些单阶段问题&#xff0c;最后将这些单阶段问题的解合并&…

坦克大战_java源码_swing界面_带毕业论文

一. 演示视频 坦克大战_java源码_swing界面_带毕业论文 二. 实现步骤 完整项目获取 https://githubs.xyz/y22.html 部分截图 启动类是 TankClinet.java&#xff0c;内置碰撞检测算法&#xff0c;线程&#xff0c;安全集合&#xff0c;一切皆对象思想等&#xff0c;是java进阶…

【vue2+antvx6】报错Cannot read properties of undefined (reading ‘toUpperCase‘)

我的代码是这样的 <el-collapseref"collapse"v-model"active"accordionclass"collapseStart"change"collapsechange"><el-collapse-item:name"String(index 1)"v-for"(i, index) in List":key"in…