C++语言基础(16)-string类
使用 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类的更多相关文章
- Java入门 - 语言基础 - 14.String类
原文地址:http://www.work100.net/training/java-string.html 更多教程:光束云 - 免费课程 String类 序号 文内章节 视频 1 概述 2 创建字符 ...
- 16 String类
java中的所有的字符串文字(例如"abc","123")都可以看做是实现了此类的实例对象 eg: String str = new String(); str ...
- C++基础之string类
string也是属于顺序容器,但是string类很重要且经常使用,因此在这里单独记录. string的操作总结 string(const char *s,int n); //用c字符串s初始化,s应 ...
- Java入门 - 语言基础 - 13.Character类
原文地址:http://www.work100.net/training/java-character.html 更多教程:光束云 - 免费课程 Character类 序号 文内章节 视频 1 概述 ...
- Java基础教程——String类
String类 Java程序中的所有字符串字面值(如 "abc" )都是String的实例 字符串是常量(因为 String 对象是不可变的,所以可以共享) 字符串的本质是字符数组 ...
- c#语言基础编程—string
引言 在c#中经常会有相关的string的操作,string类型为引用类型,集成于Object,所以会有四个方法.详情可见 值类型和引用类型的区别 里面详细介绍了,值类型和引用类型的区别和应用场合,所 ...
- Java基础笔记-String类
String 类(被final修饰) 字符串是一种特殊的对象,一旦字符串被初始化就不可以被改变了.(内容不变) 例如: String s = “abc”; String s1 = new Stri ...
- Java基础之String类
String类 字符串是不可变的,对其做的任何改变,会生成一个对象,不会改变有原有对象. ==和equals() String s1 = "good"; String s2 = & ...
- Java 基础之 String 类
String String 被声明为 final,因此不能被继承.(Integer 等包装类也不能被继承) 在 java8 中,String 内部使用 char 数组 来存储数据 public fin ...
随机推荐
- luogu P1215 [USACO1.4]母亲的牛奶 Mother's Milk
题目描述 农民约翰有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数, 最初,A和B桶都是空的,而C桶是装满牛奶的.有时,农民把牛奶从一个桶倒到另一个桶中,直到被灌桶装满或原桶空了 ...
- 【分块打表】Gym - 100923K - Por Costel and the Firecracker
semipal.in / semipal.out Por Costel the pig, our programmer in-training, has recently returned from ...
- 【2-SAT(两次DFS版)】BZOJ1823-[JSOI2010]满汉全席
[题目大意] 有n个材料,m个评委.每种材料可以被用来做满族菜或汉族菜,m个评委有两种可以让他满意的猜中.问是否可以满足所有评委要求? [思路] 每天只能做三道题,我已经是一个废人了……(葛优躺.jp ...
- 【树链剖分/线段树】BZOJ1036-[ZJOI2008]树的统计Count
[题目大意] 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成 一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX ...
- [CF321C]Ciel the Commander
题目大意: 给你一棵n个结点的树,给每个结点分级,最高为'A',最低为'Z'. 尝试构造一种分级方案,使得任意两个相同级别的结点路径上至少有一个更高级的结点. 思路: 贪心+树上点分. 递归处理每一棵 ...
- 13test02:阶乘
//假设32位int型变量y是表示最大人数的x的阶乘,即y=x!,当x最大值取什么时,y取最大值 //,且乘法不溢出. #include<iostream> using namespace ...
- Modernizr使用指南(转)
HTML5, CSS3以及相关技术(例如canvas和web sockets)带来了非常有用的特性,可以让我们的web程序提升一个新的level.这些新技术允许我们只用HTML,CSS和JavaScr ...
- CRC代码实现
CRC代码实现1: #include <stdio.h> #include <string.h> unsigned int cfgCrc32(const unsigned ch ...
- 使用Spring Boot集成FastDFS
原文:http://www.cnblogs.com/ityouknow/p/8298358.html#3893468 上篇文章介绍了如何使用Spring Boot上传文件,这篇文章我们介绍如何使用Sp ...
- linux如何启动/停止/重启MySQL
如何启动/停止/重启MySQL 一.启动方式 1.使用 service 启动:service mysqld start2.使用 mysqld 脚本启动:/etc/inint.d/mysqld star ...