字符串实战

文章目录

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;

}

`

相关推荐
疯狂的喵1 小时前
C++编译期多态实现
开发语言·c++·算法
scx201310041 小时前
20260129LCA总结
算法·深度优先·图论
2301_765703141 小时前
C++中的协程编程
开发语言·c++·算法
m0_748708051 小时前
实时数据压缩库
开发语言·c++·算法
小魏每天都学习1 小时前
【算法——c/c++]
c语言·c++·算法
lly2024062 小时前
jQuery Mobile 表格
开发语言
智码未来学堂2 小时前
探秘 C 语言算法之枚举:解锁解题新思路
c语言·数据结构·算法
惊讶的猫2 小时前
探究StringBuilder和StringBuffer的线程安全问题
java·开发语言
Halo_tjn2 小时前
基于封装的专项 知识点
java·前端·python·算法
春日见2 小时前
如何避免代码冲突,拉取分支
linux·人工智能·算法·机器学习·自动驾驶