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

news/2024/5/20 19:41:02

目录

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;

https://www.xjx100.cn/news/3366274.html

相关文章

jar包加载的位置顺序

一个基于SpringBoot的 Flink程序(FlinkListener.jar),在Windows 8系统 和 在自己的Linux虚拟机上运行正常, 把 jar 包上传到局域网Linux服务器上,提示 NoSuchMethodError错误 Caused by: java.lang.NoSuchMethodError: com.mysql.cj.CharsetMapping.getJavaEncodingForMysqlC…

Linux内核之kmemdup与kstrdup用法实例区别(三十六)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 优质专栏&#xff1a;多媒…

烂笔头笔记:Windows 11下照片查看器显示偏色问题修复

本文出处&#xff1a;http://blog.csdn.net/chaijunkun/article/details/137278931&#xff0c;转载请注明。由于本人不定期会整理相关博文&#xff0c;会对相应内容作出完善。因此强烈建议在原始出处查看此文。 最近在研究HDR视频的截图算法&#xff0c;目的就是生成色彩正确…

高效准确!指甲剪盖片视觉检测技术解密

指甲剪的盖片是指指甲剪的一端&#xff0c;通常用来盖住另一端的刀刃部分。指甲剪盖片是指甲剪的重要部分&#xff0c;除了保护刀刃外&#xff0c;还起到美观和便捷的作用。正确使用和保养指甲剪盖片可以延长指甲剪的使用寿命。 本案是对指甲剪盖片最大尺寸长75mm*宽10mm*高3mm…

【STM32嵌入式系统设计与开发】——16InputCapture(输入捕获应用)

这里写目录标题 STM32资料包&#xff1a; 百度网盘下载链接&#xff1a;链接&#xff1a;https://pan.baidu.com/s/1mWx9Asaipk-2z9HY17wYXQ?pwd8888 提取码&#xff1a;8888 一、任务描述二、任务实施1、工程文件夹创建2、函数编辑&#xff08;1&#xff09;主函数编辑&#…

[Python学习篇] Python格式化输出

输出关键字 print 语法&#xff1a;print("内容") 示例&#xff1a;print("123") 格式化符号 符号转换%s字符串%c字符%d有符号的十进制整数 &#xff08;输出正整数 和负整数-&#xff09;%03d&#xff1a;表示输出3位数字&#xff0c;如果不足3位在数字前…

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

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

Java就近原则和this关键字

Java 中的就近原则和 this 关键字有着密切的关系&#xff0c;特别是在处理成员变量与方法参数同名的情况下。就近原则指的是在同一作用域下&#xff0c;优先使用最近声明的变量或参数。 在 Java 中&#xff0c;如果一个方法的参数与类的成员变量同名&#xff0c;为了明确指示要…