string类型用法大全
- 使用标准C++中string类,要包含头文件< string >
string类的构造函数
//string(const char *s); 用字符串s初始化
string s1("hello");
//string(int n,char c); 用n个字符c初始化
string s2(10, '#');
//构造空字符串
string s3;
//赋值初始化
string s4="world";
string类的字符操作
//const char &operator[](int n)const;
cout<<s1[0]; //有可能数组越界
//const char &at(int n)const;
cout<<s1.at(1); //会进行越界检查
string的特性描述
//int capacity()const; 返回当前容量
//int max_size()const; 返回string对象中可存放的最大字符串的长度
//int size()const; 返回当前字符串的大小
cout<<s1.size();
//int length()const; 返回当前字符串的长度
cout<<s1.length();
//bool empty()const; 当前字符串是否为空
cout<<s1.empty();
string类的输入输出操作
//string类重载运算符operator>>用于输入
string s;
cin>>s;
//同样重载运算符operator<<用于输出操作。
cout<<s;
//函数getline(istream &in,string &s);用于从输入流in中读取字符串到s中,以换行符'\n'分开。
getline(cin, s);
string的赋值
//string &operator=(const string &s); 把字符串s赋给当前字符串
s1=s2;
//string &assign(const char *s); 用字符数组类型字符串s赋值
s1.assign("hello");
//string &assign(const char *s,int n); 用c字符串s开始的n个字符赋值
s1.assign("welcom", 3);
//string &assign(const string &s); 把字符串s赋给当前字符串
s1.assign(s2);
//string &assign(int n,char c); 用n个字符c赋值给当前字符串
s1.assign(10, '#');
//string &assign(const string &s,int start,int n);把字符串s中从start开始的n个字符赋给当前字符串
s1.assign(s2, 0, 2);
string的连接 ##
//string &operator+=(const string &s); 把字符串s连接到当前字符串的结尾
s1+=s2;
//string &append(const char *s); 把c类型字符串s连接到当前字符串结尾
//string &append(const char *s,int n); 把c类型字符串s的前n个字符连接到当前字符串结尾
s1.append("hello", 3);
//string &append(const string &s); 同operator+=()
s1.append(s2);
//string &append(const string &s,int pos,int n);把字符串s中从pos开始的n个字符连接到当前字符串的结尾
//string &append(int n,char c); 在当前字符串结尾添加n个字符c
string的比较
//比较两个字符串是否相等
bool operator==(const string &s1,const string &s2)const;
运算符">","<",">=","<=","!="均被重载用于字符串的比较;
int compare(const string &s) const; //比较当前字符串和s的大小
//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
int compare(int pos, int n,const string &s)const;
//比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小
int compare(int pos, int n,const string &s,int pos2,int n2)const;
int compare(const char *s) const;
int compare(int pos, int n,const char *s) const;
int compare(int pos, int n,const char *s, int pos2) const;
compare函数在>时返回1,<时返回-1,==时返回0
string的子串
string substr(int pos = 0,int n = npos) const;
//返回pos开始的n个字符组成的字符串
string的交换
void swap(string &s2); //交换当前字符串与s2的值
String类的查找函数
int find(char c, int pos = 0) const;
//从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos = 0) const;
//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;
//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string &s, int pos = 0) const;
//从pos开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值
int rfind(char c, int pos = npos) const;
//从pos开始从后向前查找字符c在当前串中的位置
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string &s,int pos = npos) const;
//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值
int find_first_of(char c, int pos = 0) const;
//从pos开始查找字符c第一次出现的位置
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) const;
//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos
int find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos,int n) const;
int find_first_not_of(const string &s,int pos = 0) const;
//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos
int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) const;
int find_last_of(const string &s,int pos = npos) const;
int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos, int n) const;
int find_last_not_of(const string &s,int pos = npos) const;
//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找
string类的替换函数
string &replace(int p0, int n0,const char *s);
//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const char *s, int n);
//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符
string &replace(int p0, int n0,const string &s);
//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const string &s, int pos, int n);
//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符
string &replace(int p0, int n0,int n, char c);
//删除p0开始的n0个字符,然后在p0处插入n个字符c
string类的插入函数
string &insert(int p0, const char *s);
string &insert(int p0, const char *s, int n);
string &insert(int p0,const string &s);
string &insert(int p0,const string &s, int pos, int n);
//前4个函数在p0位置插入字符串s中pos开始的前n个字符
string &insert(int p0, int n, char c);
//此函数在p0处插入n个字符c
string类的删除函数
string &erase(int pos = 0, int n = npos);
//删除pos开始的n个字符,返回修改后的字符串
字符串流处理
通过定义ostringstream和istringstream对象实现,需要#include <sstream>头文件
例如:
string input("hello,this is a test");
istringstream is(input);
string s1,s2,s3,s4;
is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"
ostringstream os;
os<<s1<<s2<<s3<<s4;
cout<<os.str();
string类型用法大全的更多相关文章
- C++中 string 的用法大全
之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至 ...
- go中字符串类型string的用法
示例 // 字符串类型string的用法 package main import ( "fmt" "unsafe" ) func main() { // 字符串 ...
- MVC5 + EF6 + Bootstrap3 (9) HtmlHelper用法大全(下)
文章来源:Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-httphelper-part2.html 上一节 ...
- MVC中HtmlHelper用法大全参考
MVC中HtmlHelper用法大全参考 解析MVC中HtmlHelper控件7个大类中各个控件的主要使用方法(1) 2012-02-27 16:25 HtmlHelper类在命令System.Web ...
- Python3 pandas用法大全
Python3 pandas用法大全 一.生成数据表 1.首先导入pandas库,一般都会用到numpy库,所以我们先导入备用: import numpy as np import pandas as ...
- 把《c++ primer》读薄(3-1 标准库string类型初探)
督促读书,总结精华,提炼笔记,抛砖引玉,有不合适的地方,欢迎留言指正. 问题1:养成一个好习惯,在头文件中只定义确实需要的东西 using namespace std; //建议需要什么再using声 ...
- String.format()用法
package junit.test; import java.util.Date; import java.util.Locale; import org.junit.Test; pub ...
- 所学新知——int、char型转string 类型等
1. 利用stringstream类 定义头文件#include<sstream> 通过 int a; char b; sstream ss,ss1; ss<<a; ss1&l ...
- redis的string类型
string : string类型是二进制安全的, 可以包含任何数据,比如jpg图片或者序列化的对象 . 方法 : set : 设置key对应的值为string类型的value set name ...
随机推荐
- scrapy 发送post请求
登录人人网为例 1.想要发送post请求,那么使用'scrapy.FormRequest'方法,可以方便的指定表单数据 2.如果想在爬虫一开始的时候就发送post请求,那么应该重写'start_req ...
- 谈谈IE针对Ajax请求结果的缓存
在默认情况下,IE会针对请求地址缓存Ajax请求的结果.换句话说,在缓存过期之前,针对相同地址发起的多个Ajax请求,只有第一次会真正发送到服务端.在某些情况下,这种默认的缓存机制并不是我们希望的(比 ...
- 树莓派播放网络磁盘MP3文件
树莓派映射网络磁盘 sudo mount -t cifs -o username=jerry.huang,password=Hello2014 //10.10.0.1/Public/ESLPod.co ...
- Hdoj 2109.Fighting for HDU 题解
Problem Description 在上一回,我们让你猜测海东集团用地的形状,你猜对了吗?不管结果如何,都没关系,下面我继续向大家讲解海东集团的发展情况: 在最初的两年里,HDU发展非常迅速,综合 ...
- 【BZOJ5289】[HNOI2018]排列(贪心)
[BZOJ5289][HNOI2018]排列(贪心) 题面 BZOJ 洛谷 题解 这个限制看起来不知道在干什么,其实就是找到所有排列\(p\)中,\(p_k=x\),那么\(k<j\),其中\( ...
- C#解决方案生成工具(2)
环境 VS2017 社区版 W10 Project类 : 在Microsoft.Build.Evaluation命名空间下,可使用Project类分析项目的.csproj文件 // 实例化对象,参数 ...
- Mysql 从入门到遗忘
高级数据过滤: WHERE AND OR NOT 总是与其他操作符一起使用,用在要过滤的前面. 通配符过滤: LIKE: %相当于正则中的.*?,_相当于正则中的.. $ select id from ...
- String:字符串常量池
String:字符串常量池 作为最基础的引用数据类型,Java 设计者为 String 提供了字符串常量池以提高其性能,那么字符串常量池的具体原理是什么,我们带着以下三个问题,去理解字符串常量池: 字 ...
- Win32 SDK:ListBox 为什么不整个 LB_SETTEXT
Win32 & SDK: ListBox 有个 LB_GETTEXT,为什么没有 LB_SETTEXT,想修改指定Item的Text,还真不容易. 自已写了一个,支持单选和多选模式,部分代码: ...
- (最大连续和/最大子段和) P1115 最大子段和 洛谷
题目描述 给出一段序列,选出其中连续且非空的一段使得这段和最大. 输入输出格式 输入格式: 第一行是一个正整数NN,表示了序列的长度. 第二行包含NN个绝对值不大于1000010000的整数A_iAi ...