字符串实战

文章目录

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;

}

`

相关推荐
写后端的胖头鱼3 分钟前
【高频面试题】Java 基础类型 + 包装类 + String 互相转换
java·开发语言
matlab代码26 分钟前
基于matlab自助水果超市水果识别收费系统(GUI界面)【源码67期】
开发语言·matlab
Navigator_Z27 分钟前
LeetCode //C - 1240. Tiling a Rectangle with the Fewest Squares
c语言·算法·leetcode
稻米哟27 分钟前
力扣100——双指针
算法·leetcode
hui-梦苑41 分钟前
[PHP]轻量主题函数WordPress 集成 KaTeX 数学公式渲染
开发语言·php·wordpress·katex
大圣编蚕1 小时前
Java StringWriter 详解:从入门到实战
java·开发语言·python
码行山野赴时序归途1 小时前
C语言预处理指令:编译前的“幕后导演“
c语言·开发语言
golang学习记1 小时前
Go 1.27新特性: json/v2使用有趣指南
开发语言·golang·json
老苗项目实战2 小时前
Java工程师高频面试题(基于近一年的真实面试记录整理)
java·开发语言·面试·预见猿份·老苗项目实战
小小、码农2 小时前
C++11右值引用与移动语义 —— 从拷贝资源到转移资源
开发语言·网络·c++·网络协议