字符串实战

文章目录

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;

}

`

相关推荐
qq_429879675 分钟前
省略号和可变参数模板
开发语言·c++·算法
优秀的颜1 小时前
计算机基础知识(第五篇)
java·开发语言·分布式
CodeWithMe1 小时前
【C/C++】std::vector成员函数清单
开发语言·c++
uyeonashi1 小时前
【QT控件】输入类控件详解
开发语言·c++·qt
飞川撸码1 小时前
【LeetCode 热题100】网格路径类 DP 系列题:不同路径 & 最小路径和(力扣62 / 64 )(Go语言版)
算法·leetcode·golang·动态规划
Neil今天也要学习1 小时前
永磁同步电机参数辨识算法--IPMSM拓展卡尔曼滤波全参数辨识
单片机·嵌入式硬件·算法
yzx9910132 小时前
基于 Q-Learning 算法和 CNN 的强化学习实现方案
人工智能·算法·cnn
iCxhust2 小时前
Prj10--8088单板机C语言8259测试(1)
c语言·开发语言
亮亮爱刷题2 小时前
算法练习-回溯
算法
眼镜哥(with glasses)3 小时前
蓝桥杯 国赛2024python(b组)题目(1-3)
数据结构·算法·蓝桥杯