• 使用标准C++中string类,要包含头文件< string >

string类的构造函数

  1. //string(const char *s); 用字符串s初始化
  2. string s1("hello");
  3. //string(int n,char c); 用n个字符c初始化
  4. string s2(10, '#');
  5. //构造空字符串
  6. string s3;
  7. //赋值初始化
  8. string s4="world";

string类的字符操作

  1. //const char &operator[](int n)const;
  2. cout<<s1[0]; //有可能数组越界
  3. //const char &at(int n)const;
  4. cout<<s1.at(1); //会进行越界检查

string的特性描述

  1. //int capacity()const; 返回当前容量
  2. //int max_size()const; 返回string对象中可存放的最大字符串的长度
  3. //int size()const; 返回当前字符串的大小
  4. cout<<s1.size();
  5. //int length()const; 返回当前字符串的长度
  6. cout<<s1.length();
  7. //bool empty()const; 当前字符串是否为空
  8. cout<<s1.empty();

string类的输入输出操作

  1. //string类重载运算符operator>>用于输入
  2. string s;
  3. cin>>s;
  4. //同样重载运算符operator<<用于输出操作。
  5. cout<<s;
  6. //函数getline(istream &in,string &s);用于从输入流in中读取字符串到s中,以换行符'\n'分开。
  7. getline(cin, s);

string的赋值

  1. //string &operator=(const string &s); 把字符串s赋给当前字符串
  2. s1=s2;
  3. //string &assign(const char *s); 用字符数组类型字符串s赋值
  4. s1.assign("hello");
  5. //string &assign(const char *s,int n); 用c字符串s开始的n个字符赋值
  6. s1.assign("welcom", 3);
  7. //string &assign(const string &s); 把字符串s赋给当前字符串
  8. s1.assign(s2);
  9. //string &assign(int n,char c); 用n个字符c赋值给当前字符串
  10. s1.assign(10, '#');
  11. //string &assign(const string &s,int start,int n);把字符串s中从start开始的n个字符赋给当前字符串
  12. s1.assign(s2, 0, 2);

string的连接 ##

  1. //string &operator+=(const string &s); 把字符串s连接到当前字符串的结尾
  2. s1+=s2;
  3. //string &append(const char *s); 把c类型字符串s连接到当前字符串结尾
  4. //string &append(const char *s,int n); 把c类型字符串s的前n个字符连接到当前字符串结尾
  5. s1.append("hello", 3);
  6. //string &append(const string &s); 同operator+=()
  7. s1.append(s2);
  8. //string &append(const string &s,int pos,int n);把字符串s中从pos开始的n个字符连接到当前字符串的结尾
  9. //string &append(int n,char c); 在当前字符串结尾添加n个字符c

string的比较

  1. //比较两个字符串是否相等
  2. bool operator==(const string &s1,const string &s2)const;
  3. 运算符">","<",">=","<=","!="均被重载用于字符串的比较;
  4. int compare(const string &s) const; //比较当前字符串和s的大小
  5. //比较当前字符串从pos开始的n个字符组成的字符串与s的大小
  6. int compare(int pos, int n,const string &s)const;
  7. //比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小
  8. int compare(int pos, int n,const string &s,int pos2,int n2)const;
  9. int compare(const char *s) const;
  10. int compare(int pos, int n,const char *s) const;
  11. int compare(int pos, int n,const char *s, int pos2) const;
  12. compare函数在>时返回1,<时返回-1,==时返回0

string的子串

  1. string substr(int pos = 0,int n = npos) const;
  2. //返回pos开始的n个字符组成的字符串

string的交换

  1. void swap(string &s2); //交换当前字符串与s2的值

String类的查找函数

  1. int find(char c, int pos = 0) const;
  2. //从pos开始查找字符c在当前字符串的位置
  3. int find(const char *s, int pos = 0) const;
  4. //从pos开始查找字符串s在当前串中的位置
  5. int find(const char *s, int pos, int n) const;
  6. //从pos开始查找字符串s中前n个字符在当前串中的位置
  7. int find(const string &s, int pos = 0) const;
  8. //从pos开始查找字符串s在当前串中的位置
  9. //查找成功时返回所在位置,失败返回string::npos的值
  10. int rfind(char c, int pos = npos) const;
  11. //从pos开始从后向前查找字符c在当前串中的位置
  12. int rfind(const char *s, int pos = npos) const;
  13. int rfind(const char *s, int pos, int n = npos) const;
  14. int rfind(const string &s,int pos = npos) const;
  15. //从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值
  16. int find_first_of(char c, int pos = 0) const;
  17. //从pos开始查找字符c第一次出现的位置
  18. int find_first_of(const char *s, int pos = 0) const;
  19. int find_first_of(const char *s, int pos, int n) const;
  20. int find_first_of(const string &s,int pos = 0) const;
  21. //从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos
  22. int find_first_not_of(char c, int pos = 0) const;
  23. int find_first_not_of(const char *s, int pos = 0) const;
  24. int find_first_not_of(const char *s, int pos,int n) const;
  25. int find_first_not_of(const string &s,int pos = 0) const;
  26. //从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos
  27. int find_last_of(char c, int pos = npos) const;
  28. int find_last_of(const char *s, int pos = npos) const;
  29. int find_last_of(const char *s, int pos, int n = npos) const;
  30. int find_last_of(const string &s,int pos = npos) const;
  31. int find_last_not_of(char c, int pos = npos) const;
  32. int find_last_not_of(const char *s, int pos = npos) const;
  33. int find_last_not_of(const char *s, int pos, int n) const;
  34. int find_last_not_of(const string &s,int pos = npos) const;
  35. //find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找

string类的替换函数

  1. string &replace(int p0, int n0,const char *s);
  2. //删除从p0开始的n0个字符,然后在p0处插入串s
  3. string &replace(int p0, int n0,const char *s, int n);
  4. //删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符
  5. string &replace(int p0, int n0,const string &s);
  6. //删除从p0开始的n0个字符,然后在p0处插入串s
  7. string &replace(int p0, int n0,const string &s, int pos, int n);
  8. //删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符
  9. string &replace(int p0, int n0,int n, char c);
  10. //删除p0开始的n0个字符,然后在p0处插入n个字符c

string类的插入函数

  1. string &insert(int p0, const char *s);
  2. string &insert(int p0, const char *s, int n);
  3. string &insert(int p0,const string &s);
  4. string &insert(int p0,const string &s, int pos, int n);
  5. //前4个函数在p0位置插入字符串s中pos开始的前n个字符
  6. string &insert(int p0, int n, char c);
  7. //此函数在p0处插入n个字符c

string类的删除函数

  1. string &erase(int pos = 0, int n = npos);
  2. //删除pos开始的n个字符,返回修改后的字符串

字符串流处理

  1. 通过定义ostringstreamistringstream对象实现,需要#include <sstream>头文件
  2. 例如:
  3. string input("hello,this is a test");
  4. istringstream is(input);
  5. string s1,s2,s3,s4;
  6. is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"
  7. ostringstream os;
  8. os<<s1<<s2<<s3<<s4;
  9. cout<<os.str();

string类型用法大全的更多相关文章

  1. C++中 string 的用法大全

    之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至 ...

  2. go中字符串类型string的用法

    示例 // 字符串类型string的用法 package main import ( "fmt" "unsafe" ) func main() { // 字符串 ...

  3. MVC5 + EF6 + Bootstrap3 (9) HtmlHelper用法大全(下)

    文章来源:Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-httphelper-part2.html 上一节 ...

  4. MVC中HtmlHelper用法大全参考

    MVC中HtmlHelper用法大全参考 解析MVC中HtmlHelper控件7个大类中各个控件的主要使用方法(1) 2012-02-27 16:25 HtmlHelper类在命令System.Web ...

  5. Python3 pandas用法大全

    Python3 pandas用法大全 一.生成数据表 1.首先导入pandas库,一般都会用到numpy库,所以我们先导入备用: import numpy as np import pandas as ...

  6. 把《c++ primer》读薄(3-1 标准库string类型初探)

    督促读书,总结精华,提炼笔记,抛砖引玉,有不合适的地方,欢迎留言指正. 问题1:养成一个好习惯,在头文件中只定义确实需要的东西 using namespace std; //建议需要什么再using声 ...

  7. String.format()用法

    package junit.test;   import java.util.Date; import java.util.Locale;   import org.junit.Test;   pub ...

  8. 所学新知——int、char型转string 类型等

    1. 利用stringstream类 定义头文件#include<sstream> 通过 int a; char b; sstream ss,ss1; ss<<a; ss1&l ...

  9. redis的string类型

    string : string类型是二进制安全的, 可以包含任何数据,比如jpg图片或者序列化的对象 . 方法 : set : 设置key对应的值为string类型的value set  name   ...

随机推荐

  1. SPHINX 文档写作工具安装简要指南 - windows 版 - 基于python

    此教程基于本地己安装好 PYTHON 并配置过全局变量:一定具备相应的基础再操作: 上传图片以免产生误导,以下为文字描述,按下列操作即可: 下载 get-pip.py脚本; python get-pi ...

  2. 概念数据模型CDM基础

    概念数据模型CDM 概念数据模型是设计数据库不可或缺的一步,是整个数据库设计的关键,CDM的主要作用如下: 1)能够真实地模拟真实世界,是需求分析人员和数据库设计人员沟通的桥梁.2)将系统需求分析得到 ...

  3. android 读取EXcel 文件 读取文件内存卡的权限

    android 采用Java的读取xls文件的方式实现. 需要导入第三方Jxl.jar 包.  代码改自 其他博主  : 这只摘录下 读取xls文件的部分代码,当然这个代码在安卓平台需要添加下面的权限 ...

  4. 【BZOJ5250】[九省联考2018]秘密袭击(动态规划)

    [BZOJ5250][九省联考2018]秘密袭击(动态规划) 题面 BZOJ 洛谷 给定一棵树,求其所有联通块的权值第\(k\)大的和. 题解 整个\(O(nk(n-k))\)的暴力剪剪枝就给过了.. ...

  5. luogu5021 [NOIp2018]赛道修建 (二分答案+dp(贪心?))

    首先二分一下答案,就变成了找长度>=m的 不相交的路径的个数 考虑到在一个子树中,只有一个点能出这个子树去和别的点搞 所以我这个子树里尽量自我满足是不会有坏处的 而且要在自我满足数最大的条件下, ...

  6. NOIp2018 游记

    作为一名蒟蒻,对于NOIp当然是不抱什么希望.所以就只能在比赛中吸取经验咯... Day0 害怕书到用时方恨少,疯狂打板子(玩电脑) Day1 来到考场了,发现键盘空格按不起,觉得非常尴尬,然后他告诉 ...

  7. [THUWC2017]在美妙的数学王国中畅游

    [THUWC2017]在美妙的数学王国中畅游 e和sin信息不能直接合并 泰勒展开,大于21次太小,认为是0,保留前21次多项式即可 然后就把e,sin ,kx+b都变成多项式了,pushup合并 上 ...

  8. 没有上司的舞会 codevs 1380

    上树DP,记忆化搜索. 本题老师讲的方法是直接树形DP,但是由于我对树并不够了解,什么dfs也不想尝试(虽然感觉自己可以搞),于是搞了个结构体存点以及该点的信息,用f[i][j]作为记忆化数组.以后最 ...

  9. A1111. Online Map

    Input our current position and a destination, an online map can recommend several paths. Now your jo ...

  10. Django 项目内利用ORM直接运行脚本读库

    #导包 import os import sys #将脚本所在工程添加到环境变量 #绝对路径 # sys.path.append('c:/Users/nxy/www/mymac') #相对路径 sys ...