当前位置: 首页 > news >正文

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;

相关文章:

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

目录 1.c_str() 返回C常量字符串 2.date() 返回C常量字符串 3.substr() 构造子串 4.find() 正向查找&#xff08;查找失败返回npos&#xff09; 5.rfind() 逆向查找&#xff08;查找失败返回npos&#xff09; 6.find_first_of() 正向查找匹配的字符 7.find_last_of() 逆向…...

Java编程使用CGLIB动态代理介绍与实战演示

文章目录 前言技术积累核心概念主要功能适用场景与JDK动态代理的对比 实战演示定义待代理的目标类实现MethodInterceptor接口使用代理对象 测试结果写在最后 前言 在Java编程中&#xff0c;CGLIB (Code Generation Library) 是一个强大的高性能代码生成库&#xff0c;它通过生…...

vue3 渲染一个后端返回的图片字段渲染、table表格内放置图片

一、后端直接返回图片url 当图片字段接口直接返回的是图片url&#xff0c;可以直接放到img标签上 <img v-if"thumbLoader" class"r-image-loader-thumb" :src"resUrl" /> 二、当图片字段接口直接返回的是图片Id 那么就需要去拼一下图片…...

iOS开发进阶(十三):脚手架创建iOS项目

文章目录 一、前言二、xcode-select 命令三、拓展阅读 一、前言 项目初期&#xff0c;需要搭建项目基本框架&#xff0c;为此离不开辅助工具&#xff0c;即脚手架。当然&#xff0c;IDE也可以实现新建空白项目&#xff0c;但是其新建后的项目结构可能不符合预期设计&#xff0…...

手机无线投屏到windows11电脑

1 安装无线投影组件 2 电脑端打开允许其他设备投影的开关 3 手机找到投屏选项 4 手机搜索可用设备连接即可 这里的官方文档给的不太好,给了一些让人眼花撩乱的信息,以下是经过整合的有效信息...

linux 环境安装配置

安装java17 1.下载安装包 wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz 2.解压到自定义目录/usr/local/java mkdir /usr/local/java tar zxvf jdk-17_linux-x64_bin.tar.gz -C /usr/local/java 3.配置环境变量 echo export PATH$PATH:/…...

Git常用语句

设置用户名 git config --global user.name "用户名" git config --global user.email "邮箱"查看git用户信息 cat ~/.gitconfig初始化本地库 git initclone指定分支的代码 git clone -b my_branch gitgitlabxxxxxxxxxxxxxxxxxxxxxx.gitpush三件套 gi…...

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

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

JVM 记录

记录 工具 https://gceasy.io 资料 尚硅谷宋红康JVM全套教程&#xff08;详解java虚拟机&#xff09; https://www.bilibili.com/video/BV1PJ411n7xZ?p361 全套课程分为《内存与垃圾回收篇》《字节码与类的加载篇》《性能监控与调优篇》三个篇章。 上篇《内存与垃圾回收篇…...

Linux学习笔记————C 语言版 LED 灯实验

这里写目录标题 一、实验程序编写二、 汇编部分实验程序编写三、C 语言部分实验程序编写四、编译下载验证 汇编 LED 灯实验中&#xff0c;我们讲解了如何使用汇编来编写 LED 灯驱动&#xff0c;实际工作中是很少用到汇编去写嵌入式驱动的&#xff0c;毕竟汇编太难&#xff0c;而…...

Spring Boot 配置文件

1. 配置文件的作用 配置文件主要是为了解决硬件编码带来的问题&#xff0c;把可能会发生改变的信息&#xff0c;放在一个集中的地方&#xff0c;当我们启动某个程序时&#xff0c;程序从配置文件中读取一些数据&#xff0c;并加载运行。 硬编码是将数据直接放在源代码中&…...

IPKISS ------ 查看器件默认端口名称

IPKISS ------ 查看器件默认端口名称 正文正文 我们这里以 Grating Coupler 举例。 import si_fab.all as pdk import ipkiss3.all as i3class MyGratingCoupler(i3.circuit):gc = i3.childcellProperty(<...

uni-app踩坑记录

uni-app踩坑记录 Failed to load local image resource xxx the server responded with a status of 500 (HTTP/1.1 500 Internal Server Error) Failed to load local image resource xxx the server responded with a status of 500 (HTTP/1.1 500 Internal Server Error) 文…...

【嵌入式硬件】光耦

1.光耦作用 光耦一般用于信号的隔离。当两个电路的电源参考点不相关时,使用光耦可以保证在两边不共地的情况下,完成信号的传输。 2.光耦原理 光耦的原理图如下所示,其内部可以看做一个特殊的“三极管”; 一般的三极管是通过基极B和发射极E间的电流,去控制集电极C和发射极…...

学习Fast-LIO系列代码中相关概念理解

目录 一、流形和流形空间&#xff08;姿态&#xff09; 1.1 定义 1.2 为什么要有流形? 1.3 流形要满足什么性质&#xff1f; (1) 拓扑同胚 (2) 可微结构 1.4 欧式空间和流形空间的区别和联系? (1) 区别&#xff1a; (2) 联系&#xff1a; 1.5 将姿态定义在流形上比…...

React 掌握及对比常用的8个Hooks,优化及使用场景

1、useState 在函数组件中&#xff0c;可以使用useState来定义函数组件的状态。使用useState来创建状态。 1.引入2.接收一个参数作为初始值3.返回一个数组&#xff0c;第一个值为状态&#xff0c;第二个值为改变状态的函数 2、 useEffect useEffect又称副作用hooks。作用&…...

DNS域名解析过程

在互联网中我们通信目标是对方的IP&#xff0c;但是由于IP不便于记忆所以引入了域名 域名和IP是一一对应的关系&#xff0c;需要注意的是域名和网址是不同的概念 比如&#xff1a;www.csdn.net是域名&#xff0c;https://www.csdn.net/?spm1001.2101.3001.4476是网址 首先了解…...

MySQL数据库(数据库连接池)

文章目录 1.批处理应用1.基本介绍2.批处理演示1.创建测试表2.修改url3.编写java代码 3.批处理源码分析 2.数据库连接池1.传统连接弊端分析2.数据库连接池基本介绍1.概念介绍2.数据库连接池示意图3.数据库连接池种类 3.C3P0连接池1.环境配置1.导入jar包2.将整个lib添加到项目中3…...

【C#】知识点速通

前言&#xff1a; 笔者是跟着哔站课程&#xff08;Trigger&#xff09;学习unity才去学习的C#&#xff0c;并且C语言功底尚存&#xff0c;所以只是简单地跟着课程将unity所用的C#语言的关键部分进行了了解&#xff0c;然后在后期unity学习过程中加以深度学习。如需完善的C#知识…...

FTP协议

FTP协议 客户端向服务器发送文件。 C/S架构。 运行在TCP/IP协议上面。 FTP客户端要和FTP服务端建立两个TCP连接。 控制连接&#xff1a;运行在整个连接过程&#xff0c;传输控制信息。 数据连接&#xff1a;在每次文件传输时才会建立&#xff0c;文件传输完就关闭。 主动模式…...

未来机器人的大脑:如何用神经网络模拟器实现更智能的决策?

编辑&#xff1a;陈萍萍的公主一点人工一点智能 未来机器人的大脑&#xff1a;如何用神经网络模拟器实现更智能的决策&#xff1f;RWM通过双自回归机制有效解决了复合误差、部分可观测性和随机动力学等关键挑战&#xff0c;在不依赖领域特定归纳偏见的条件下实现了卓越的预测准…...

ESP32 I2S音频总线学习笔记(四): INMP441采集音频并实时播放

简介 前面两期文章我们介绍了I2S的读取和写入&#xff0c;一个是通过INMP441麦克风模块采集音频&#xff0c;一个是通过PCM5102A模块播放音频&#xff0c;那如果我们将两者结合起来&#xff0c;将麦克风采集到的音频通过PCM5102A播放&#xff0c;是不是就可以做一个扩音器了呢…...

Spring Boot面试题精选汇总

&#x1f91f;致敬读者 &#x1f7e9;感谢阅读&#x1f7e6;笑口常开&#x1f7ea;生日快乐⬛早点睡觉 &#x1f4d8;博主相关 &#x1f7e7;博主信息&#x1f7e8;博客首页&#x1f7eb;专栏推荐&#x1f7e5;活动信息 文章目录 Spring Boot面试题精选汇总⚙️ **一、核心概…...

【HarmonyOS 5 开发速记】如何获取用户信息(头像/昵称/手机号)

1.获取 authorizationCode&#xff1a; 2.利用 authorizationCode 获取 accessToken&#xff1a;文档中心 3.获取手机&#xff1a;文档中心 4.获取昵称头像&#xff1a;文档中心 首先创建 request 若要获取手机号&#xff0c;scope必填 phone&#xff0c;permissions 必填 …...

Redis的发布订阅模式与专业的 MQ(如 Kafka, RabbitMQ)相比,优缺点是什么?适用于哪些场景?

Redis 的发布订阅&#xff08;Pub/Sub&#xff09;模式与专业的 MQ&#xff08;Message Queue&#xff09;如 Kafka、RabbitMQ 进行比较&#xff0c;核心的权衡点在于&#xff1a;简单与速度 vs. 可靠与功能。 下面我们详细展开对比。 Redis Pub/Sub 的核心特点 它是一个发后…...

HarmonyOS运动开发:如何用mpchart绘制运动配速图表

##鸿蒙核心技术##运动开发##Sensor Service Kit&#xff08;传感器服务&#xff09;# 前言 在运动类应用中&#xff0c;运动数据的可视化是提升用户体验的重要环节。通过直观的图表展示运动过程中的关键数据&#xff0c;如配速、距离、卡路里消耗等&#xff0c;用户可以更清晰…...

SiFli 52把Imagie图片,Font字体资源放在指定位置,编译成指定img.bin和font.bin的问题

分区配置 (ptab.json) img 属性介绍&#xff1a; img 属性指定分区存放的 image 名称&#xff0c;指定的 image 名称必须是当前工程生成的 binary 。 如果 binary 有多个文件&#xff0c;则以 proj_name:binary_name 格式指定文件名&#xff0c; proj_name 为工程 名&…...

【7色560页】职场可视化逻辑图高级数据分析PPT模版

7种色调职场工作汇报PPT&#xff0c;橙蓝、黑红、红蓝、蓝橙灰、浅蓝、浅绿、深蓝七种色调模版 【7色560页】职场可视化逻辑图高级数据分析PPT模版&#xff1a;职场可视化逻辑图分析PPT模版https://pan.quark.cn/s/78aeabbd92d1...

20个超级好用的 CSS 动画库

分享 20 个最佳 CSS 动画库。 它们中的大多数将生成纯 CSS 代码&#xff0c;而不需要任何外部库。 1.Animate.css 一个开箱即用型的跨浏览器动画库&#xff0c;可供你在项目中使用。 2.Magic Animations CSS3 一组简单的动画&#xff0c;可以包含在你的网页或应用项目中。 3.An…...

[免费]微信小程序问卷调查系统(SpringBoot后端+Vue管理端)【论文+源码+SQL脚本】

大家好&#xff0c;我是java1234_小锋老师&#xff0c;看到一个不错的微信小程序问卷调查系统(SpringBoot后端Vue管理端)【论文源码SQL脚本】&#xff0c;分享下哈。 项目视频演示 【免费】微信小程序问卷调查系统(SpringBoot后端Vue管理端) Java毕业设计_哔哩哔哩_bilibili 项…...