基本使用方法

一、输入

  1. string s
  2. cin >> s;
    getline(cin, s) ; //使用默认的'\n'作为终止符
  3. getline(cin, s, '!') ; //以'!'作为终止符

二、复制

  1. string s1 = "hello World" ;
  2. string s2 = s1 ; //"hello World" 复制,
  3. string s3(s1); //"hello World" 拷贝构造函数,
  4. string s4(s1, ); //"llo World" 将s1的第2个位置到末尾当作字符串的初值
  5. string s5(s1, , ); //"lo Wo" 将s1的第2个位置开始的5个字符作为字符串的初值
  6. string s8(, 'a'); //“aaaaa” 生成一个字符串,包含5个c字符

三、连接

  1. string s1 = "Hello" ;
  2. string s2 = "World" ;string s3, s4;
  3.  
  4. s1 += s2 ; //"HelloWorld" 连接
  5. s3 = string("aaa") + "bbb"; //"aaabbb"
  6. s4 = "aaa" + "bbb"; //错误, 必须转成string类型!!!!!

四、比较

  1. string s1 = "hello" ;
  2. string s2 = "world" ;
  3. if(s1 < s2)
  4. cout<<"s1 < s2" ; //比较

五、倒置串

  1. string s = "hello" ;
  2. reverse(s.begin(), s.end()) ; //需要包含algorithm头文件, #include<algorithm> 

    

六、查找串

  1. string s1 = "hhhelloworlddd" ; //位置从0 ~ 9
  2. s1.find("w") ; // 7 找第1个w的位置
  3. s1.find("w", ) ; // -1 从第10个位置开始找w
  4.  
  5. s1.find_first_of("o"); //6 找第1次出现"o"的位置
  6. s1.find_first_of("o",); //8 从s1的第7个位置开始查找
  7. s1.find_first_not_of("h"); //3 找第1个不是"h"的字符的位置
  8.  
  9. s1.find_last_of("o"); //8 //从后面开始找"o"
  10. s1.find_last_of("o", ); //6 //从后面7个字符中找"o"出现的位置
  11. s1.find_last_not_of("d"); //

七、替换和字串

  1. string s1,s2,s3;
  2. s1 = "hello world!" ;
  3. s2 = s1.substr(, ); //"lo wo" 从第3个位置开始,往后5个字符
  4. s3 = s1.substr(); //"world!" 从第6个字符到末尾部分
  5. s1.replace(, , "tt"); //"hettorld" 从第2个位置,往后5个字符换成“tt”

八、修改字符串

  1. ①. append - 追加
  2. string s = "hello" ;
  3. s.append("world") ; //将"world"追加到s中
  4.  
  5. ②. push_back - 追加字符到字符串
  6. string s = "hello" ;
  7. s.push_back('!') ; //将'!'追加字符到字符串s中
  8.  
  9. ③. insert - 插入
  10. string s = "hello" ;
  11. s.insert(2, "www") ; //将字符串"www"插入到字符串s中, 插入位置为2
  12.  
  13. ④. erase - 从字符串中擦除一些字符
  14. string s = "hello" ;
  15. s.erase(1, 2) ; //从下标为1处向后擦去2个字符
  16.  
  17. ⑤. swap - 与另一字符串交换内容
  18. string s1 = "hello" ;
  19. string s2 = "world" ;
  20. s1.swap(s2) ; //将s1与s2中的字符串进行交换

 

九、获取字符串状态

  1. s.size() //返回字符串大小
  2. s.length() //返回字符串长度
  3. s.max_size() //返回字符串最大长度
  4. s.clear() //清空字符串
  5. s.empty() //判断字符串是否为空

十、string中的所有s1都替换成s2

  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. //"12212"这个字符串的所有"12"都替换成"21"
  7. string& replace_all( string str, const string& old_value, const string& new_value ) //替换为22211
  8. {
  9. while( true )
  10. {
  11. string::size_type pos( );
  12. if( ( pos = str.find( old_value ) ) != string::npos )
  13. {
  14. str.replace( pos, old_value.length(), new_value );
  15. }
  16. else
  17. {
  18. break;
  19. }
  20. }
  21. return str;
  22. }
  23.  
  24. string& replace_all_distinct( string str, const string& old_value, const string& new_value ) //替换为21221
  25. {
  26. for( string::size_type pos( ); pos != string::npos; pos += new_value.length() )
  27. {
  28. if( ( pos = str.find( old_value, pos ) ) != string::npos )
  29. {
  30. str.replace( pos, old_value.length(), new_value );
  31. }
  32. else
  33. {
  34. break;
  35. }
  36. }
  37. return str;
  38. }
  39.  
  40. int main()
  41. {
  42. cout << replace_all( string(""), "", "" ) << endl; //22211
  43. cout << replace_all_distinct( string(""), "", "" ) << endl; //21221
  44. }

STL --> string类字符串的更多相关文章

  1. C++标准模板库Stand Template Library(STL)简介与STL string类

    参考<21天学通C++>第15和16章节,在对宏和模板学习之后,开启对C++实现的标准模板类STL进行简介,同时介绍简单的string类.虽然前面对于vector.deque.list等进 ...

  2. hiho1482出勤记录II(string类字符串中查找字符串,库函数的应用)

    string类中有很多好用的函数,这里介绍在string类字符串中查找字符串的函数. string类字符串中查找字符串一般可以用: 1.s.find(s1)函数,从前往后查找与目标字符串匹配的第一个位 ...

  3. 《java入门》第一季之类(String类字符串一旦被赋值就没法改变)

    毫无疑问,String类是java里面最重要的类之一.因此它有很多方法需要了解和掌握. 字符串一旦被赋值,值就不能发生改变: package cn.itcast_02; /* * 字符串的特点:一旦被 ...

  4. 常用类一一字符串相关类一一String类 字符串的使用

    Java字符串就是Unicode字符序列,例如“Java”就是4个Unicode字符J,a,v,a组成的. Java没有内置的字符串类型,而是在标准Java类库中提供了一个预定义的类String,每个 ...

  5. 关于C语言打印string类字符串的问题

    首先因为printf函数输出字符串是针对char *的,即printf只能输出c语言的内置数据,而string不是c语言的内置数据. 其次string类型的对象不止包含字符串,还包含了许多用于操作的函 ...

  6. 01.String类字符串本质

    String类是在java开发过程中,使用最最频繁的一个类,不管是 用户名 密码 还是http报文接收过来的数据,其本质就是字符序列 所以做为一个java开发者,我们要重点掌握好String的方法使用 ...

  7. C++STL—string类

    string容器 1.1 string容器的基本概念 string容器是一个类 这个容器中有一个指针,指针维护了一个数组 string容器提供copy.find.insert.replace等等功能 ...

  8. 萌新笔记——C++里将string类字符串(utf-8编码)分解成单个字(可中英混输)

    最近在建词典,使用Trie字典树,需要把字符串分解成单个字.由于传入的字符串中可能包含中文或者英文,它们的字节数并不相同.一开始天真地认为中文就是两个字节,于是很happy地直接判断当前位置的字符的A ...

  9. String类字符串截取示范

    package it.com; // 要求:對字符串“jflksjdfnbalkdfjnbaddddnbahhuhnbauuuuahnbahdfunbadhfudf”进行检索:判断有多少个nba; / ...

随机推荐

  1. DirectShow使用说明

    1) 安装directX sdk 2)打开安装目录下的dshow.dsw,进行编译 3)在VC++的Tools/Option/Directory的Include和Library中分别加入 C:/DXS ...

  2. 部署Java Web项目报错(一)

    今天,我在部署Java Web项目时,出现错误,并且在eclipse新建一个servers,却出现多个项目. 具体错误截图如下: 然后,我又将项目部署到JBoss服务器中,却还是运行不成功 22:12 ...

  3. [Err] 1136 - Column count doesn't match value count at row 1

    1 错误描述 [Err] 1136 - Column count doesn't match value count at row 1 Procedure execution failed 1136 ...

  4. Caused by: java.lang.ClassNotFoundException: org.aspectj.lang.annotation.Around

    1.错误描述 INFO:2015-05-01 11:12:15[localhost-startStop-1] - Root WebApplicationContext: initialization ...

  5. Error: expected expression, got '}'

    1.错误描述 Error: expected expression, got '}' .globalEval/<@http://localhost:8080/Sys/resource/globa ...

  6. C# 图解教程 第五章 方法

    方法的结构方法体内部代码的执行本地变量    类型推断和var关键字    嵌套块中的本地变量本地常量控制流方法调用返回值返回语句和void方法参数    形参    实参值参数引用参数引用类型作为值 ...

  7. Fu+ 后台管理 (Thinkphp)

    简要:小主从事PHP二年,期间一直做后台;为此向大家分享我制作一个后台(权限),希望能够跟各位PHP大神学习探索,如果有不对或者好的建议告知下:*~*! 1. 介绍 Fu+ 后台管理,是本人基于H+好 ...

  8. 在VCS仿真器中使用FSDB

    FSDB(Fast Signal Database)是Verdi支持的文件格式,用于保存仿真产生的信号波形.据Verdi文档说明,FSDB比标准的VCD格式节省磁盘空间,处理速度更快.要用VCS仿真器 ...

  9. 【BZOJ3944】Sum(杜教筛)

    [BZOJ3944]Sum(杜教筛) 题面 求\[\sum_{i=1}^n\mu(i)和\sum_{i=1}^n\phi(i)\] 范围:\(n<2^{31}\) 令\[S(n)=\sum_{i ...

  10. 【BZOJ1901】【Luogu2617】Dynamic Ranking(主席树,树状数组)

    [BZOJ1901][Luogu2617]Dynamic Ranking(主席树,树状数组) 题面 神TM BZOJ权限题 Luogu真良心 题解 如果不考虑修改 很容易的主席树区间第K大 考虑修改 ...