字符串实战

文章目录

c类型字符串
定义
遍历
c++类型str
定义
字符串拼接
获取指定位置的字符
遍历
截取字符串
获取字符在字符串指定位置的索引

`

#include

#include

using namespace std;

int main(){

// c风格的字符串

char hellostr[] = {'h','e','l','l','o','\0'};

char hellostr2[] = "hello2";

cout << hellostr << endl;

cout << hellostr2 << endl;

/*

遍历字符串

*/

for (int i = 0; i < sizeof(hellostr)/sizeof(char); ++i) {

cout << i<< "==" << hellostr[i] << endl;

}

//定义

string name = "abcde";

cout << name << endl;

​ //拼接

cout << name + "123" << endl;

​ // 获取2号位置的字符

cout << "第2个位置的字符"<<name[1] <<endl;

cout << "2号位置的字符" <<name.at(1) << endl;

​ // 遍历

for (int i = 0; i < name.size(); ++i) {

cout << "第"<< i <<"个位置的值="<< name[i]<<endl;

}

​ for(char c:name){

cout << c << endl;

}

​ //截取字符串

cout<< "截取下标0到3(不包括)的数据" <<name.substr(0,3)<<endl;

​ //获取字符串的中的索引

cout <<" b在字符串中的索引" <<name.find('b')<< endl;

​ return 0;

}

`

out <<" b在字符串中的索引" <<name.find('b')<< endl;

​ return 0;

}

`

相关推荐
BIGSHU09234 分钟前
java多线程场景3-并发处理和异步请求
java·开发语言·python
索迪迈科技5 分钟前
算法题(203):矩阵最小路径和
线性代数·算法·矩阵
默默无名的大学生10 分钟前
数据结构——链表的基本操作
数据结构·算法
_OP_CHEN13 分钟前
数据结构(C语言篇):(十一)二叉树概念介绍
c语言·开发语言·数据结构·二叉树·学习笔记··
Neverfadeaway15 分钟前
C语言————冒泡排序(例题2)
c语言·数据结构·算法·冒泡排序·升序排列·降序排列
柯南二号23 分钟前
【设计模式】【观察者模式】实例
java·开发语言·servlet
沐怡旸28 分钟前
【底层机制】稀疏文件--是什么、为什么、好在哪、实现机制
c++·面试
索迪迈科技32 分钟前
Java-Spring入门指南(四)深入IOC本质与依赖注入(DI)实战
java·开发语言·spring
惊鸿.Jh32 分钟前
1733. 需要教语言的最少人数
算法·leetcode
向依阳34 分钟前
C++:类和对象
c++·类和对象