字符串实战

文章目录

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;

}

`

相关推荐
星辰烈龙14 分钟前
黑马程序员Java基础9
java·开发语言
San3019 分钟前
从零到一:彻底搞定面试高频算法——“列表转树”与“爬楼梯”全解析
javascript·算法·面试
F_D_Z25 分钟前
最长连续序列(Longest Consecutive Sequence)
数据结构·算法·leetcode
ss27326 分钟前
Java并发编程:DelayQueue延迟订单系统
java·python·算法
SHERlocked9328 分钟前
摄像头 RTSP 流视频多路实时监控解决方案实践
c++·后端·音视频开发
JHC00000028 分钟前
118. 杨辉三角
python·算法·面试
@游子32 分钟前
Python类属性与魔术方法全解析
开发语言·python
WolfGang00732143 分钟前
代码随想录算法训练营Day50 | 拓扑排序、dijkstra(朴素版)
数据结构·算法
业精于勤的牙1 小时前
浅谈:算法中的斐波那契数(四)
算法
一直都在5721 小时前
数据结构入门:二叉排序树的删除算法
数据结构·算法