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 ...
随机推荐
- py thon 多线程(转一篇好文章)
http://www.cnblogs.com/fnng/p/3670789.html
- [BZOJ 2817] 波浪
Link: BZOJ 2817 传送门 Solution: 算是比较神的DP了吧, 首先这个绝对值处理起来很难受,肯定要想办法去掉 于是想到从小到大插入的方式,便不存在绝对值的问题了 插入一个数只有5 ...
- [BZOJ 1150] 数据备份
Link:https://www.lydsy.com/JudgeOnline/problem.php?id=1150 Solution: 思路和洛谷P1484完全相同 只不过将求最大不相邻的点权改为最 ...
- 【主席树】bzoj2588 Spoj 10628. Count on a tree
每个点的主席树的root是从其父转移来的.询问的时候用U+V-LCA-FA(LCA)即可. #include<cstdio> #include<algorithm> using ...
- 【DFS序】【莫队算法】【权值分块】bzoj2809 [Apio2012]dispatching
题意:在树中找到一个点i,并且找到这个点子树中的一些点组成一个集合,使得集合中的所有点的c之和不超过M,且Li*集合中元素个数和最大 首先,我们将树处理出dfs序,将子树询问转化成区间询问. 然后我们 ...
- 【暴力】vijos P1897 学姐吃牛排
判断堆:递归判断每个节点的孩子是否都比其父亲大(小). 判断BST:中序遍历是否有序. #include<cstdio> using namespace std; #define lc ( ...
- 6.1(java学习笔记)File类
1.路径分隔符,文件分隔符. 路径分隔符(“:”) 文件名称分隔符(“\”windows,“/”Linux等). 不同平台使用的文件分隔符是不一样的,所以File类中提供了分隔符常量,它会根据平台的不 ...
- Linux下判断字符串长度
方法1:使用wc -L命令 wc -L可以获取到当前行的长度,因此对于单独行的字符串可以用这个简单的方法获取,另外wc -l则是获取当前字符串内容的行数. echo 'abc' |wc -L 注意:这 ...
- Ado.Net基础拾遗二:插入,更新,删除数据
插入数据 public void InsertDataToSQL() { string conStr = ConfigurationManager.ConnectionStrings["No ...
- nodeJs的模块依赖
1.require载入依赖 var http = require('http');//http为路径,可以忽略后缀 2.exports输出依赖 exports.add=add;//add为方法 3.只 ...