栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ string用法总结

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ string用法总结

string

1、头文件

#include

2、初始化

string s{"hello world"};
string s1{6,'z'};//zzzzzz
string s2{s};
string s3{"hello world",5};//hello
string s4{"hello world",7,5};//world,开始位置,字符个数

3、添加

//添加子串
string s5=s+s;//hello worldhello world
string s6=s+"hello world";//hello worldhello world
//不能 s6="hello"+"hello",两个字面意义上的字符串不能相加

4、删除

s.erase(0,6);//world,开始索引,删除字符个数
s.clear();//清除

5、截取

string s7=s.substr(7,5);//world,从第7位截取5个字符
string s8=s.substr(5);//hello

6、搜索

int index=s.find("world");//6
int index2=s.find('h');//0
index2=s.find('z');
if (index2 == string::npos)//判断不存在
{
	cout << "no" << endl;
}
s.rfind('o')//7,逆向搜索

7、插入

s.insert(11,"hello world");//插入开始位置,插入开始位置可以是字符串最后一位的后一位

8、替换

s.replace(6,5,"me");//hello me.将从6开始的之后5个字符替换成me

9、char类型与string

s2 =s+ 'a' + 'b';
string s9{ 'c' };
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/976586.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号