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 ...
随机推荐
- python requests的安装与简单运用(转)
http://www.cnblogs.com/fightformylife/p/4134986.html http://cn.python-requests.org/zh_CN/latest/ htt ...
- wildfly 10的安装部署
http://www.xue163.com/2203/1/22037981_2.html WildFly 曾用名:JBoss Application Server ,红帽公司宣布 JBoss AS 的 ...
- [USACO Special 2007 Chinese Competition]The Bovine Accordion and Banjo Orchestra
[原题描述以及提交地址]:http://acm.tongji.edu.cn/problem?pid=10011 [题目大意] 给定两个长度为N的序列,要给这两个序列的数连线.连线只能在两个序列之间进行 ...
- [BZOJ 1566] 管道取珠
Link:https://www.lydsy.com/JudgeOnline/problem.php?id=1566 Solution: 思路十分精奇的一道题目 题目要求的是$\sum_{i=1}^k ...
- 指定等级 Exercise07_01
import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:指定等级 * */ public class Exercise07_01 ...
- jQuery判断一个元素是否为另一个元素的子元素
判断:当前元素是否是被筛选元素的子元素 jQuery.fn.isChildOf = function(b){ return (this.parents(b).length > 0); }; 判断 ...
- SVN安装中遇到的问题
新的版本:1.9.5 必须使用Apache Portable Runtime Utility 1.5.4 Released没有安装的话需要先安装 需要安装apr.apr-util sqlite zli ...
- 设置html属性为disabled时flask后台获取数据失败
标签input的值如果不需要用户修改,则设置属性为 readonly,不要设置为 disabled.因为设置disabled会导致flask后端获取不到这个input得value rule_maker ...
- javascript数据类型检测方法
一.字符串.数字.布尔值.undefined的最佳选择市使用 typeof 运算符进行检测: 对于字符串,typeof 返回"string" 对于数字,typeof 返回" ...
- elasticsearch中的filter与aggs
今天在ES上做了一个聚合,先过滤一个嵌套对象,再对另一个域做聚合,但是过滤似乎没有起作用 { "size":0, "filter":{ "nested ...