深入理解C++中substr()函数的使用
创作时间:
作者:
@小白创作中心
深入理解C++中substr()函数的使用
引用
CSDN
1.
https://blog.csdn.net/2201_75539691/article/details/145100333
在C++语言的学习中,字符串处理是一个非常重要的内容,而substr()函数作为std::string类中的成员函数,为我们提供了提取子字符串的便捷操作。在本文中,我们将深入解析substr()函数的用法,包括其函数原型、参数意义、使用场景以及常见的错误和注意事项。同时,还会拓展substr()与其他函数(如find())的配合使用,以帮助读者更好地理解和掌握字符串操作的技巧。
本文内容基于一节关于substr()函数的课程整理而成,并进行了优化和拓展,以便于初学者和进阶学习者参考。
函数概述
substr()函数用于截取字符串中指定位置和长度的子串。其函数原型如下:
string substr(size_t pos = 0, size_t len = npos) const;
参数说明:
- pos: 指定子字符串的起始位置(从0开始计数)。如果未传入参数,则默认从字符串的起点(pos = 0)开始。
- len: 子字符串的长度。若未传入参数,则默认值为npos,表示从起始位置一直截取到字符串的末尾。
返回值:
返回截取得到的子字符串,类型为std::string。
基础用法示例
示例1:从指定位置提取到字符串末尾
以下代码展示了如何从字符串的某个位置开始提取到末尾的子串:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello world hello everyone";
string s1 = s.substr(7);
cout << s1 << endl; // 输出: "orld hello everyone"
return 0;
}
解析:
- s.substr(7)表示从下标7处(字符o)开始,一直截取到字符串的末尾。
- 输出结果为:orld hello everyone。
示例2:从指定位置提取固定长度的子串
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello world hello everyone";
string s2 = s.substr(7, 5);
cout << s2 << endl; // 输出: "orld "
return 0;
}
解析:
- s.substr(7, 5)表示从下标7处开始,截取长度为5的子串。
- 输出结果为:orld 。
substr()与find()函数的结合
示例3:配合find()提取子串
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello world hello everyone";
size_t pos = s.find("world");
if (pos != string::npos) {
string s3 = s.substr(pos, 5);
cout << s3 << endl; // 输出: "world"
}
return 0;
}
解析:
- 使用find()函数查找"world"的位置,然后使用substr()函数从该位置截取长度为5的子串。
- 输出结果为:world。
注意事项
越界问题
示例4:越界示例
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello world hello everyone";
try {
string s4 = s.substr(20, 10);
cout << s4 << endl;
} catch (const out_of_range& e) {
cout << "Error: " << e.what() << endl;
}
return 0;
}
解析:
- 尝试从位置20开始截取长度为10的子串,但由于字符串长度不足,会抛出out_of_range异常。
- 输出结果为:Error: basic_string::substr。
find()的返回值校验
示例5:find()未找到子串
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello world hello everyone";
size_t pos = s.find("test");
if (pos == string::npos) {
cout << "Substring not found" << endl;
}
return 0;
}
解析:
- 使用find()函数查找"test",由于字符串中不存在该子串,返回值为npos。
- 输出结果为:Substring not found。
扩展阅读:npos的意义
示例6:npos的基本使用
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello world hello everyone";
size_t pos = s.find("world");
if (pos != string::npos) {
string s6 = s.substr(pos);
cout << s6 << endl; // 输出: "world hello everyone"
}
return 0;
}
解析:
- 使用find()函数查找"world"的位置,如果找到则使用substr()函数从该位置截取到字符串末尾的子串。
- 输出结果为:world hello everyone。
小结
通过本文的讲解,相信读者已经对substr()函数有了全面的了解。substr()函数在字符串处理中非常实用,但使用时需要注意边界条件,避免引发异常。同时,与其他字符串处理函数(如find())的结合使用,可以实现更复杂的字符串操作需求。
热门推荐
周易的风水理念
人工智能(AI)在工业设计领域的应用
SOP是什么(企业管理中的标准化操作程序详解)
正常成人血压标准范围是多少
江西女子跟风网红摆摊卖火鸡面,出摊2天就放弃了:根本不赚钱
在 Windows 中启用和禁用触摸键盘
小炒黄牛肉:经典湘菜的详细烹饪指南
公司法人与财务分开的法律框架及实务操作
304不锈钢一般应用在什么领域?
退休,对你意味着什么?
说说便秘那些事儿
外贸路由器选购攻略:助力外贸企业连接全球市场
联合国徽记与旗帜:设计、使用与历史渊源
世界十大佛塔:建筑之美与文化之韵
基因突变是什么意思
无乳糖牛奶有哪些?品牌、营养与口味全解析
注塑熔接痕产生原因及解决策略
有哪些面料的背带裤膝盖不易鼓包?
糖尿病患者能喝牛奶吗?这份选购指南请收好
PPT的主要用途和应用场景
FOF基金的运作模式是怎样的?这种运作模式有哪些优缺点?
五声调式音阶及钢琴练习方法
自粘防水卷材施工工艺流程详解
哪些法律途径可以追回损失
风景摄影构图全攻略:从入门到精通
肝脏问题就医指南:从症状识别到治疗方案
“无痛分娩”知多少?
1989年苏联再次组建机枪炮兵师,共有7个,如今有些还在俄军序列
公理与定理的区别
健康科普丨中医特色疗法——艾灸