字符串实战

文章目录

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;

}

`

相关推荐
scx201310048 分钟前
20250814 最小生成树和重构树总结
c++·算法·最小生成树·重构树
阿巴~阿巴~17 分钟前
冒泡排序算法
c语言·开发语言·算法·排序算法
散11229 分钟前
01数据结构-交换排序
数据结构·算法
yzx9910131 小时前
Yolov模型的演变
人工智能·算法·yolo
看到我,请让我去学习2 小时前
QT - QT开发进阶合集
开发语言·qt
weixin_307779132 小时前
VS Code配置MinGW64编译SQLite3库
开发语言·数据库·c++·vscode·算法
无聊的小坏坏2 小时前
拓扑排序详解:从力扣 207 题看有向图环检测
算法·leetcode·图论·拓扑学
wwww.bo2 小时前
机器学习(决策树)
算法·决策树·机器学习
辞--忧2 小时前
深入浅出决策树
算法·决策树·机器学习
Y200309162 小时前
决策树总结
算法·决策树·机器学习