字符串实战

文章目录

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<< "==" << hellostri << endl;

}

//定义

string name = "abcde";

cout << name << endl;

​ //拼接

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

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

cout << "第2个位置的字符"<<name1 <<endl;

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

​ // 遍历

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

cout << "第"<< i <<"个位置的值="<< namei<<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;

}

`

相关推荐
fqbqrr8 小时前
2606C++,C++构的多态
开发语言·c++
biter down9 小时前
从 0 到 1 搭建 Python 接口自动化测试框架(博客系统实战)
开发语言·python
小欣加油9 小时前
leetcode56 合并区间
c++·算法·leetcode·职场和发展
lqqjuly9 小时前
前沿算法深度解析(二)
人工智能·算法·机器学习
Yolo_TvT9 小时前
C++:析构函数
c++
徐小夕10 小时前
万字长文!千万级文档 RAG 知识库系统落地实践
前端·算法·github
threelab10 小时前
Three.js 物理模拟着色器 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器
武器大师7210 小时前
lv_binding_js 代码解读
开发语言·javascript·ecmascript
不知名的老吴10 小时前
线程的生命周期之线程“插队“
java·开发语言·python