1、按照面向对象的要求,可以把字符串看作一个对象,设计一个串类加以描述。但是,在一般的情况下不必建立自己的串类,c++标准

在库<string>中给出了类string,提供了丰富的串操作,程序员使用指令: #include<string>即存取这个类。可将类string视为一个容器类,其部分操作如下:

构造函数:

string s:构造一个空串s

string s(a):构造串s,并用字符数组a初始化

string s(a,n):构造串s,用字符数组a的前n个字符初始化

string s(str):用串str构造串s(拷贝初始化)

看下面代码验证

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{ char a[maxn];
cin>>a;//输入abcdefgh
string s(a);//结果为abcdefgh
string s1(a,);//结果为abcde
string s2(s1);//结果为abcde
cout<<s<<endl;
cout<<s1<<endl;
cout<<s2<<endl;
return ;
}

输入输出:

getline(cin,s,delim):从cin中提取字符存入串s中,当遇到delim或者提取的字符个数等于s的长度时结束,如果自己不设定,默认为'\n'

getline(cin,s):遇到'\n'结束

cin>>s:遇到空白字符终止提取,空白字符不被读取

cout<<s:输出s

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{ string s;
while(getline(cin,s,'#'))
{
getchar();//自己指定了字符要加getchar,不然结果会多出一个换行,但是默认情况下不用加getchar
cout<<s<<endl;
}
return ;
}

操作符:

s=val:赋值操作,val可以是一个串 ,一个字符或一个字符数组

s+=val:将val添加到串s之后

s[pos]:下标操作

s+t,t+s:s和t的连接,返回连接的结果。 t可以是一个串,一个字符或者一个字符数组

t<s,s<t,s<=t:由关系操作符确定结果是true还是false

t==s,s==t,s!+t······

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{ string s1,s2;
char a;
for(int i=;i<;i++)
{
cin>>a;
s1+=a;
}
cout<<s1<<endl;//输入为abcde,结果为abcde
cin>>s2;//输入fg
cout<<s2<<endl;//输出fg
s1+=s2;
cout<<s1<<endl;//输出abcdefg
if(s1<s2) cout<<"yes"<<endl;
else cout<<"no"<<endl;
return ;
}

添加:

s.append(str):将str添加在s之后,返回s

s.append(str,pos,n):将str中从位置pos开始的n个字符添加在s之后,如果n太大,则取str中从位置pos到串结尾的字符,返回s

s.append(a):将字符数组添加在s之后,返回s

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{ string s1,s2;
cin>>s1>>s2;//输入abcde fg
cout<<s1<<" "<<s2<<endl;//输出abcde fg
s1.append(s2);
cout<<s1<<endl;//输出abcdefg
s2.append(s1,,);//输出结果为fgbcd
cout<<s2<<endl;
return ;
}

赋值:

s.assign(str):用str给s赋值,返回s

s.assign(str,pos,n):将str中从位置pos开始的n个字符组成的字串赋给s

s.assign(a):将字符数组赋给s

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{ string s1,s2;
cin>>s1;//输入abcde
s2.assign(s1);
cout<<s2<<endl;//输出结果为abcde
s2.assign(s1,,);
cout<<s2<<endl;//输出结果为abc
return ;
}

长度:

s.size():返回s的长度,不能用于统计C风格的以'\0'结尾的字符串长度

s.length():返回s的长度,不能用于统计C风格的以'\0'结尾的字符串长度

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{ string s1,s2;
cin>>s1;//输入abcde
cout<<s1.length()<<endl;
cout<<s1.size()<<endl;
return ;
}

比较:
s.compare(str):根据s小于,等于,大于,返回一个负值,0,正值

s.compare(a):像上面的一样,只是这里是一个字符数组a而已

s.compare(pos,n,str):s中从位置pos开始的n个字符组成的子串与str比较

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{ string s1,s2;
cin>>s1>>s2;
cout<<s1.compare(s2)<<endl;
cout<<s1.compare(,,s2)<<endl;
return ;
}

插入:

s.insert(pos,str):在s的位置pos处插入str

s.insert(pos1,str,pos2,n):将str中从位置pos2开始的n个字符插入s中的位置pos1处

s.insert(pos,a,n):将a 的前n个字符插入s中位置pos处

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{ string s1,s2;
cin>>s1>>s2;//输入abcde
s1.insert(,s2);
cout<<s1<<endl;
s1.insert(,s2,,);//位置都是从0开始的
cout<<s1<<endl;
return ;
}

替换:

s.replace(pos1,n1,str):用str替换s中从位置pos1开始的n1个字符组成的子串,返回s

s.replace(pos1,n1,str,pos2,n2):用str中从位置pos2开始的n2个字符替换s中从位置pos1开始的n1个字符,返回s

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{ string s1,s2;
cin>>s1>>s2;//输入abcde fg
s1.replace(,,s2);//结果为afgde
cout<<s1<<endl;
s1.replace(,,s2,,);
cout<<s1<<endl;//结果为fgde
return ;
}

取子串:

s.substr(pos,n):返回从pos开始的n个字符组成的子串,如果n太大或者省略,则返回从pos开始直到s结尾的左右字符组成的子串,pos的默认值为0,会在末尾自动加'\0'

c语言的strncpy函数不会在末尾帮你加'\0',切记要自己加

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{ string s1,s2;
cin>>s1;//输入abcde
cout<<s1.substr(,)<<endl;//结果为bcd
return ;
}

是否为空串:

s.empty:为空,返回true,不为空,返回false

查找:

s.find(str,pos):在串s中从位置pos开始查找串str,若查找成功,返回str首次出现的位置,若查找不成功,返回s.npos,pos的默认值为0

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
string s="abc";
if(s.find('d')!=s.npos) cout<<""<<endl;
else cout<<""<<endl;
return ;
}
//输出为0

s.find(a,pos):同上

s.rfind(str,pos):在s的前(pos+str.size())个字符中查找str,若查找成功,返回str最后一次出现的位置,若不成功,返回-1,若pos省略,则在s中查找str最后一次出现的位置

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{ string s1,s2;
cin>>s1>>s2;//输入abcde bc cout<<s1.find(s2,);
return ;
}
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{ string s1,s2;
cin>>s1>>s2;//输入abcdefgabcdefg fg
cout<<s1.rfind(s2)<<endl;//
cout<<s1.rfind(s2,)<<endl;//
return ;
}

reverce()函数:将字符串翻转,s.reverce(s.begin(),s.end())

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std; #include <iostream>
#include <string>
#include <algorithm>
using namespace std; int main()
{
string s = "hello"; reverse(s.begin(),s.end()); cout<<s<<endl; return ;
}

C++的string类提供下列3个成员函数将一个string类的对象s转换成C风格的字符串:

s.c_str():将存储在s中的字符作为字符数组返回,并添加一个空字符('\0')作为结束符

s.data():将存储在s中的字符作为字符数组返回,最后不添加('\0')

s.copy(a,n,pos):用s中从位置pos开始的n个字符替换a的前n个字符(如果n太大,则用从pos开始的n个字符直到s的末尾的所有字符),pos默认为0,返回用于替换的字符个数

string的各种函数(系统学习)的更多相关文章

  1. 新手C#string类常用函数的学习2018.08.04

    ToLower()用于将字符串变为小写,注意字符串的不可变特性,需要重新赋值给另一个字符串变量. s = s.ToLower();//字符串具有不可变性,转换后需要重新赋值,不可仅有s.ToLower ...

  2. C++系统学习之六:函数

    1.函数基础 典型的函数定义包括:返回类型.函数名.由0个或多个形参组成的列表以及函数体. 2.参数传递 形参初始化的机理和变量初始化一样. 有两种方式:引用传递和值传递 2.1 传值参数 当形参是非 ...

  3. Haskell 笔记(四)函数系统

    函数系统 函数式编程当然少不了函数系统啦,在教程最初的时候就有一个最简单的函数,函数系统贯穿在Haskell全部,Haskell的函数有几个重要的性质. 首先声明一下函数的参数和返回值类型 然后有一个 ...

  4. 【系统学习ES6】第一节:新的声明方式

    [系统学习ES6] 本专题旨在对ES6的常用技术点进行系统性梳理,帮助大家对其有更好的掌握.计划每周更新1-2篇,希望大家有所收获. 以前用ES5时,声明变量只能用var.ES6的出现,为我们带来了两 ...

  5. 【系统学习ES6】第二节:解构赋值

    [系统学习ES6] 本专题旨在对ES6的常用技术点进行系统性梳理,帮助大家对其有更好的掌握,希望大家有所收获. ES6允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构.解构是一种打 ...

  6. Hive自定义函数的学习笔记(1)

    前言: hive本身提供了丰富的函数集, 有普通函数(求平方sqrt), 聚合函数(求和sum), 以及表生成函数(explode, json_tuple)等等. 但不是所有的业务需求都能涉及和覆盖到 ...

  7. C++ string类及其函数的讲解

    文章来源于:http://www.cnblogs.com/hailexuexi/archive/2012/02/01/2334183.html C++中string是标准库中一种容器,相当于保存元素类 ...

  8. Linux系统学习笔记:文件I/O

    Linux支持C语言中的标准I/O函数,同时它还提供了一套SUS标准的I/O库函数.和标准I/O不同,UNIX的I/O函数是不带缓冲的,即每个读写都调用内核中的一个系统调用.本篇总结UNIX的I/O并 ...

  9. Hibernate的系统 学习

    Hibernate的系统 学习 一.Hibernate的介绍 1.什么是Hibernate? 首先,hibernate是数据持久层的一个轻量级框架.数据持久层的框架有很多比如:iBATIS,myBat ...

  10. Dubbo -- 系统学习 笔记 -- 快速启动

    Dubbo -- 系统学习 笔记 -- 目录 快速启动 服务提供者 服务消费者 快速启动 Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubb ...

随机推荐

  1. C# 性能总结

    尽量使用using语句块和finally (实现IDisposable) 尽量使用单个大程序集而不是多个小程序集 (使用NGen.exe) 使用sealed关键字 权衡虚函数 使用弱引用

  2. HDOJ1114(完全背包)

    #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const ...

  3. Android中EditTex焦点设置和弹不弹出输入法的问题(转)

    今天编程碰到了一个问题:有一款平板,打开一个有EditText的Activity会默认弹出输入法.为了解决这个问题就深入研究了下android中焦点Focus和弹出输入法的问题.在网上看了些例子都不够 ...

  4. oracle--事物---

    一.什么是事务 事务用于保证数据的一致性,它由一组相关的dml语句组成,该组的dml(数据操作语言,增删改,没有查询)语句要么全部成功,要么全部失败. 如:网上转账就是典型的要用事务来处理,用于保证数 ...

  5. JavaScript代码存在形式

    <!-- 方式一 --> <script type"text/javascript" src="JS文件"></script> ...

  6. mkfs在特定的分区上建立 linux 文件系统

    Linux mkfs命令用于在特定的分区上建立 linux 文件系统 使用方式 : mkfs [-V] [-t fstype] [fs-options] filesys [blocks]参数 :    ...

  7. Learning Python 003 缩进

    Python 缩进 Python的代码中不使用{}大括号来来表示一个代码块,而是使用缩进方式.像下面这段代码: # print absolute value of an integer: a = 10 ...

  8. neon eclipse tomcat发布项目乱码

    解决方法如图

  9. StringBuffer输出

    public class Test { public static void main(String[] args) { StringBuffer a = new StringBuffer(" ...

  10. Firefox 网页 光标 闪烁

    最近 Firefox 出现怪异情况:鼠标点击网页,在点击的位置显示光标,并一直闪烁,导致 Home End 等按键都无效. 原来这是 Firefox 的 “特色功能”:Caret Browsing,激 ...