博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++语言基础(16)-string类
阅读量:4591 次
发布时间:2019-06-09

本文共 2978 字,大约阅读时间需要 9 分钟。

 

使用 string 类需要包含头文件<string>,下面的例子介绍了几种定义 string 变量(对象)的方法:

#include 
#include
using namespace std;int main(){ string s1; // 只定义不初始化,编译器会赋默认值,默认值为"",即空字符串 string s2 = "C Plus Plus"; // 既定义又初始化,与C风格字符串不同,string结尾没有结束标志'\0' string s3 = s2; // s3定义时使用s2进行初始化,因此s3内容也是"C Plus Plus" string s4 (5,'s'); // s4被初始化由5个's'字符组成的字符串,也就是"sssss" return 0;}

一.length() 返回字符串长度

string s = "http://www.iosfan.cn";int len = s.length();cout<
<

注意: 与C不同,string末尾没有'\0'字符,所以length返回的是字符串的真实长度,而不是长度+1

二.c_str() 转换为C风格的字符串

string path = "D:\\demo.txt";FILE *fp = fopen(path.c_str(), "rt");

三.访问字符串中的字符

#include 
#include
using namespace std;int main(){ string s = "1234567890"; for(int i=0,len=s.length(); i

四.字符串拼接

#include 
#include
using namespace std;int main(){ string s1 = "first "; string s2 = "second "; char *s3 = "third "; char s4[] = "fourth "; char ch = '@'; string s5 = s1 + s2; string s6 = s1 + s3; string s7 = s1 + s4; string s8 = s1 + ch; cout<
<
<
<
<
<
<
<

运行结果:

first second
first third
first fourth
first @

五.insert() 插入字符串

#include 
#include
using namespace std;int main(){ string s1, s2, s3; s1 = s2 = "1234567890"; s3 = "aaa"; s1.insert(5, s3); cout<< s1 <

运行结果

12345aaa67890

12345bbb67890

六 erase() 删除string中的一个子字符串

#include 
#include
using namespace std;int main(){ string s1, s2, s3; s1 = s2 = s3 = "1234567890"; s2.erase(5); s3.erase(5, 3); cout<< s1 <

运行结果:

1234567890

12345
1234590

七 substr() 截取字符中的一个子字符串

#include 
#include
using namespace std;int main(){ string s1 = "first second third"; string s2; s2 = s1.substr(6, 6); cout<< s1 <

运行结果:

first second third

second

八 find() 查找某个字符串出现的位置

#include 
#include
using namespace std;int main(){ string s1 = "first second third"; string s2 = "second"; int index = s1.find(s2,5); if(index < s1.length()) cout<<"Found at index : "<< index <

运行结果

Found at index : 6

九.rfind() 从第二个参数开始往后查找

#include 
#include
using namespace std;int main(){ string s1 = "first second third"; string s2 = "second"; int index = s1.rfind(s2,6); if(index < s1.length()) cout<<"Found at index : "<< index <

运行结果:

Found at index : 6

十 find_first_of() 查找子字符串首次出现的位置

#include 
#include
using namespace std;int main(){ string s1 = "first second second third"; string s2 = "asecond"; int index = s1.find_first_of(s2); if(index < s1.length()) cout<<"Found at index : "<< index <

运行结果:

Found at index : 3

 

转载于:https://www.cnblogs.com/yongdaimi/p/7097791.html

你可能感兴趣的文章
larabel Artisan Command 使用总结
查看>>
mysql中查看一个字段中,有几个逗号
查看>>
C#中的常识
查看>>
安装SQL Server 2012 『企业中文版』
查看>>
win10 上安装虚拟机
查看>>
Mysql 数据迁移后 启动出错
查看>>
HDU 2473 Junk-Mail Filter 删点并查集
查看>>
HDU - 5491 The Next 2015 ACM/ICPC Asia Regional Hefei Online
查看>>
java常用的几种线程池比较
查看>>
psutil安装
查看>>
ES5扩展属性
查看>>
Django 应用开发(3)
查看>>
自己写的java实现的多路搜索树 B-Tree
查看>>
Snow Footprints CodeForces - 298A
查看>>
Choose and divide UVA - 10375
查看>>
Delete Them
查看>>
JSON跨域原理实现
查看>>
wiringPi 库下用C控制GPIO
查看>>
Excel-单条件和多条件匹配搜索
查看>>
Android bluetooth low energy (ble) writeCharacteristic delay callback
查看>>