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

#include <iostream>
#include <string> 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 (,'s'); // s4被初始化由5个's'字符组成的字符串,也就是"sssss" return ;
}

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

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

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

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

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

三.访问字符串中的字符

#include <iostream>
#include <string>
using namespace std; int main(){
string s = "";
for(int i=,len=s.length(); i<len; i++){
cout<<s[i]<<" ";
}
cout<<endl;
s[] = '';
cout<<s<<endl;
return ;
}

四.字符串拼接

#include <iostream>
#include <string>
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<<s5<<endl<<s6<<endl<<s7<<endl<<s8<<endl; return ;
}

运行结果:
first second
first third
first fourth
first @

五.insert() 插入字符串

#include <iostream>
#include <string>
using namespace std; int main(){
string s1, s2, s3;
s1 = s2 = "";
s3 = "aaa";
s1.insert(, s3);
cout<< s1 <<endl;
s2.insert(, "bbb");
cout<< s2 <<endl;
return ;
}

运行结果

12345aaa67890
12345bbb67890

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

#include <iostream>
#include <string>
using namespace std; int main(){
string s1, s2, s3;
s1 = s2 = s3 = "";
s2.erase();
s3.erase(, );
cout<< s1 <<endl;
cout<< s2 <<endl;
cout<< s3 <<endl;
return ;
}

运行结果:

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

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

运行结果:

first second third
second

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

#include <iostream>
#include <string>
using namespace std; int main(){
string s1 = "first second third";
string s2 = "second";
int index = s1.find(s2,);
if(index < s1.length())
cout<<"Found at index : "<< index <<endl;
else
cout<<"Not found"<<endl;
return ;
}

运行结果
Found at index : 6

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

#include <iostream>
#include <string>
using namespace std; int main(){
string s1 = "first second third";
string s2 = "second";
int index = s1.rfind(s2,);
if(index < s1.length())
cout<<"Found at index : "<< index <<endl;
else
cout<<"Not found"<<endl;
return ;
}

运行结果:

Found at index : 6

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

#include <iostream>
#include <string>
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 <<endl;
else
cout<<"Not found"<<endl;
return ;
}

运行结果:

Found at index : 3

C++语言基础(16)-string类的更多相关文章

  1. Java入门 - 语言基础 - 14.String类

    原文地址:http://www.work100.net/training/java-string.html 更多教程:光束云 - 免费课程 String类 序号 文内章节 视频 1 概述 2 创建字符 ...

  2. 16 String类

    java中的所有的字符串文字(例如"abc","123")都可以看做是实现了此类的实例对象 eg: String str = new String(); str ...

  3. C++基础之string类

    string也是属于顺序容器,但是string类很重要且经常使用,因此在这里单独记录. string的操作总结 string(const char *s,int n);  //用c字符串s初始化,s应 ...

  4. Java入门 - 语言基础 - 13.Character类

    原文地址:http://www.work100.net/training/java-character.html 更多教程:光束云 - 免费课程 Character类 序号 文内章节 视频 1 概述 ...

  5. Java基础教程——String类

    String类 Java程序中的所有字符串字面值(如 "abc" )都是String的实例 字符串是常量(因为 String 对象是不可变的,所以可以共享) 字符串的本质是字符数组 ...

  6. c#语言基础编程—string

    引言 在c#中经常会有相关的string的操作,string类型为引用类型,集成于Object,所以会有四个方法.详情可见 值类型和引用类型的区别 里面详细介绍了,值类型和引用类型的区别和应用场合,所 ...

  7. Java基础笔记-String类

    String 类(被final修饰) 字符串是一种特殊的对象,一旦字符串被初始化就不可以被改变了.(内容不变) 例如: String  s = “abc”; String  s1 = new Stri ...

  8. Java基础之String类

    String类 字符串是不可变的,对其做的任何改变,会生成一个对象,不会改变有原有对象. ==和equals() String s1 = "good"; String s2 = & ...

  9. Java 基础之 String 类

    String String 被声明为 final,因此不能被继承.(Integer 等包装类也不能被继承) 在 java8 中,String 内部使用 char 数组 来存储数据 public fin ...

随机推荐

  1. python 连接数据库练习

    #!/usr/bin/python# -*- coding:utf-8 -*-import logginglogging.basicConfig(level=logging.INFO)import m ...

  2. [xsy2363]树

    设$f_{i,j}$表示$i$个点的树,权值为$j$且可以不选根的方案数,$g_{i,j}$表示$i$个点的树,权值为$j$且必选根的方案数 首先$g_{1,1}=0$ 我们可以把原树连上一个新的子树 ...

  3. 【二分答案】【Heap-Dijkstra】bzoj2709 [Violet 1]迷宫花园

    显然最短路长度随着v的变化是单调的,于是可以二分答案,据说spfa在网格图上表现较差. #include<cstdio> #include<cstring> #include& ...

  4. DataSnap Session expired处理。

    测试环境:RAD 10.2.3 建立DataSet Server服务端连接oracle数据库. 1.客户端用FDConnection连接服务端,协议为TCP/IP时,当服务端重启,不用再重启客户端. ...

  5. js创建json对象

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. mysql用unix时间戳统计一个日期段的数据

    select DATE_FORMAT(FROM_UNIXTIME(date),'%Y-%m-%d') as d, count(*) as c from tb where (FROM_UNIXTIME( ...

  7. linux-更改文件属性-chattr与lsattr

    chattr命令的用法:chattr [ -RVf ] [ -v version ] [ mode ] 文件 最关键的是在[mode]部分,[mode]部分是由+-=和[ASacDdIijsTtu]这 ...

  8. 图解http读书笔记

    以前对HTTP协议一知半解,一直不清楚前端需要对于HTTP了解到什么程度,知道接触的东西多了,对于性能优化.服务端的配合和学习中也渐渐了解到了HTTP基础的重要性,看了一些大神对HTTP书籍的推荐,也 ...

  9. 《VC++就业培训宝典之MFC视频教程》学习笔记

    开发环境:Win7 64位 + VS2013 第三章第二节视频 在Win32工程中学习MFC开发,编译问题: Building MFC application with /MD[d] (CRT dll ...

  10. 客户端连接Redis

    首先下载Jedis http://mvnrepository.com/artifact/redis.clients/jedis 然后脚本如下: package redistest; import ja ...