字符串实战

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;
}

`

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值