C++常用string函数
来自https://www.cnblogs.com/jm-Xu/p/9318705.html
string(s小写)是C++标准库中的类,纯C中没有,使用时需要包含头文件#include<string>
string的定义及初始化
string s1 = "hello"; //初始化字符串
string s2 ("world"); //另一种初始化 string s3; //初始化字符串,空字符串 string s4(, 'a'); //s4由连续5个a组成,即s4="aaaaa";
string s5(s1,,); //从s1的2位置的字符开始,连续3个字符赋值给s5,即s5="llo";
string s6(s1, ); //从s1的2位置的字符开始,将后续的所有字符赋值给s6,即s6="ello";
重载的运算符
此处列举一下被重载的运算符,基本意思一目了然。其中注意“+”操作
s1 = s2;
s1 += s2;
s1 = s2 + s3;
s1 == s2;
s1 = "s" + s2; //正确
s1 = "s" + "s"; //错误,加号两边至少要有一个string类型的对象
s1 = "s" + s2 + "s" + "s"; //正确 “+”的两边要保证至少有一个string类型,所以5正确,6错误。由于在C/C++中,+的返回值还是原类型,所以第7行中,"s"+s2返回一个string类型,因此string+“s”也是正确的,
遍历string(迭代器)
遍历string中的元素时,我们可以使用类似C中的数组形式访问,如s1[](下标从0开始) 也可以使用STL特有的迭代器访问:
string::iterator it;
for (it = s1.begin(); it != s1.end(); it++){
cout << *it << endl;
} cout << *(s1.begin()); //正确,即访问s1[0]
cout << *(s1.end()); //错误,s1.end()指向了空
若想要从后向前遍历string时,可以用到rbegin()和rend()函数。
(参考vector博文)

插入insert()
string s1 = "hello";
s1.insert(,"ins"); //从s1的1位置开始,插入"ins"字符串,即s1="hinsello";
s1.insert(, "ins", );//从s1的1位置开始,插入"ins"字符串的前2个字符,即s1="hinello";
s1.insert(, "ins", , );//从s1的1位置开始,插入"ins"字符串的从1位置开始的2个字符,即s1="hnsello"; iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
删除erase()
iterator erase(iterator first, iterator last);//删除[first,last)之间的所有字符,返回删除后迭代器的位置
iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置
string &erase(int pos = , int n = npos);//删除pos开始的n个字符,返回修改后的字符串 查找 find()
cout << s.find("aa", ) << endl; //返回的是子串位置。第二个参数是查找的起始位置,如果找不到,就返回string::npos
if (s.find("aa1", ) == string::npos)
{
cout << "找不到该子串!" << endl;
}
C++常用string函数的更多相关文章
- Python 常用string函数
Python 常用string函数 字符串中字符大小写的变换 1. str.lower() //小写>>> 'SkatE'.lower()'skate' 2. str.upper ...
- string类(二、常用string函数)
常用string相关,参至System.String类: 1/ string.Length a.Length字符串长度 string a="a5"; //a.Length==2 s ...
- 常用string函数分析
string函数分析string函数包含在string.c文件中,经常被C文件使用.1. strcpy函数原型: char* strcpy(char* str1,char* str2);函数功能: 把 ...
- php常用string函数
http://www.w3school.com.cn/php/php_ref_string.asp1.addcslashes() 返回在指定的字符前添加反斜杠的字符串2.addslashes() 返回 ...
- string中常用的函数
string中常用的函数 发现在string在处理这符串是很好用,就找了一篇文章放在这里了.. 用 string来代替char * 数组,使用sort排序算法来排序,用unique 函数来去重1.De ...
- string常用成员函数
string常用成员函数 std::string::clear Clear string Erases the contents of the string, which becomes an emp ...
- php常用字符串函数小结
php内置了98个字符串函数(除了基于正则表达式的函数,正则表达式在此不在讨论范围),能够处理字符串中能遇到的每一个方面内容,本文对常用字符串函数进行简单的小结,主要包含以下8部分:1.确定字符串长度 ...
- byte数据的常用操作函数[转发]
/// <summary> /// 本类提供了对byte数据的常用操作函数 /// </summary> public class ByteUtil { ','A','B',' ...
- c++:string函数
string类的构造函数:string(const char *s); //用c字符串s初始化string(int n,char c); //用n个字符c初始化此外,string类还支持 ...
随机推荐
- python判断一个数是不是完全平方数
思路: 完全平方数开根号后是一个整数,非完全平方数开根号的话是一个非整数 开根号后取整,如果开根号后是整数的话就不会改变值的大小 取整后再平方,如果值和之前一样,说明是完全平方数 import mat ...
- powershell 删除7天前的文件
powershell 删除7天前的文件 $today=Get-Date #"今天是:$today" #昨天 #"昨天是:$($today.AddDays(-1))&quo ...
- MYSQL中的UNION和UNION ALL
SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每 ...
- linux小白家教学<一>
<数据中心规划与实施> 教学大纲 编写人:Allen 一. 课程教学内容及目标: (一) 知识目标 1.掌握企业级LINUX部署以及相关配置: 2.掌握LINUX操作系统基本的创建.删除 ...
- 安装gitlab ce
切换到root用户,安装相关依赖 yum install curl policycoreutils openssh-server openssh-clients service sshd restar ...
- C数据结构排序算法——直接插入排序法用法总结(转http://blog.csdn.net/lg1259156776/)
声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 排序相关的的基本概念 排序:将一组杂乱无章的数据按一定的规律顺次排列起来. 数据表( data list): ...
- Vue-cli脚手架起步
1.安装node.js 下载地址:http://nodejs.cn/download/ 测试是否安装成功 node -V 检测安装包 npm -v 检测npm 2.安装webpack npm inst ...
- Nginx配置,请求到tomcat中
一.web服务器分为两类 1.web服务器 1)Apache服务器 2)Nginx 3)IIS 2.web 应用服务器 1)tomcat 2)resin 3)jetty 区分:web服务器不能解析js ...
- js 格式化 json 字符串
1.JSON.stringify的三个参数 var json = {"@odata.context":"$metadata#AddTableOne_466281s&quo ...
- CentOS MySql5.6编译安装
生产环境中,mysql服务器上边最好什么服务都不要再安装!!! 一.准备工作: # yum -y install make gcc-c++ cmake bison-devel ncurses-deve ...