11 string容器 - 子串获取

功能描述:从字符串中获取想要的子串

函数原型:

string substr(int pos = 0, int n = npos) const; //返回由pos开始的n个字符组成的字符串

cpp 复制代码
#include<iostream>
using namespace std;

// string 求子串

void test01() {

	string str = "abcdef";

	string subStr = str.substr(1, 3);

	cout << "subStr = " << subStr << endl;

}

// 实用操作
void test02() {
	string email = "zhangsan@gmail.com";

	// 从邮箱地址中 获取 用户信息

	int pos = email.find("@"); // @下标为8

	string usrName = email.substr(0, pos); // [0,8) 输出 zhangsan

	cout << "usrName = " << usrName << endl;
}

int main() {
	test01();
	test02();
	system("pause");
	return 0;
}

总结:灵活的运用求子串功能,可以在实际开发中获取有效的信息

相关推荐
fengfuyao9851 天前
基于STM32的4轴步进电机加减速控制工程源码(梯形加减速算法)
网络·stm32·算法
无敌昊哥战神1 天前
深入理解 C 语言:巧妙利用“0地址”手写 offsetof 宏与内存对齐机制
c语言·数据结构·算法
小白菜又菜1 天前
Leetcode 2075. Decode the Slanted Ciphertext
算法·leetcode·职场和发展
Proxy_ZZ01 天前
用Matlab绘制BER曲线对比SPA与Min-Sum性能
人工智能·算法·机器学习
黎阳之光1 天前
黎阳之光:以视频孪生领跑全球,赋能数字孪生水利智能监测新征程
大数据·人工智能·算法·安全·数字孪生
小李子呢02111 天前
前端八股6---v-model双向绑定
前端·javascript·算法
史迪仔01121 天前
[QML] QML IMage图像处理
开发语言·前端·javascript·c++·qt
2301_822703201 天前
Flutter 框架跨平台鸿蒙开发 - 创意声音合成器应用
算法·flutter·华为·harmonyos·鸿蒙
cmpxr_1 天前
【C】数组名、函数名的特殊
c语言·算法
KAU的云实验台1 天前
【算法精解】AIR期刊算法IAGWO:引入速度概念与逆多元二次权重,可应对高维/工程问题(附Matlab源码)
开发语言·算法·matlab