字符串实战

文章目录

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;

}

`

相关推荐
Robot_Nav9 分钟前
贪心算法、动态规划与 MPPI 算法结构相关力扣题目汇总
算法·贪心算法·动态规划
灯澜忆梦10 分钟前
GO_函数_2
开发语言·golang·xcode
apihz40 分钟前
经纬度制作高清卫星图片免费 API 接口详解
开发语言·ios·swift
战族狼魂43 分钟前
每日一课:算法系统学习路线
人工智能·算法·大模型·大语言模型
变量未定义~1 小时前
连通块中点的数量、堆箱子(4星)
算法
娇气的红酒1 小时前
微软对PHP支持的改进,及其它一些胡言乱语
开发语言·microsoft·php
霸道流氓气质1 小时前
Harness Engineering 模块化指令实战:告别 600 行巨型 AGENTS.md
开发语言·人工智能·python
盐焗鹌鹑蛋1 小时前
【C++】set和map
c++
j7~1 小时前
【算法】专题二:滑动窗口之水果成蓝,找到字符串中所有字⺟异位词等算法题
c++·算法·滑动窗口·水果成蓝·最小字串覆盖·优选算法精选
木木子221 小时前
# 星座配对深度解析:Select 长列表选择器、矩阵评分算法与数据驱动建议
java·算法·华为·矩阵·harmonyos