基本使用方法

一、输入

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

二、复制

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

三、连接

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

四、比较

string s1 = "hello" ;
string s2 = "world" ;
if(s1 < s2)
cout<<"s1 < s2" ; //比较

五、倒置串

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

    

六、查找串

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

七、替换和字串

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

八、修改字符串

①. append - 追加
string s = "hello" ;
s.append("world") ; //将"world"追加到s中 ②. push_back - 追加字符到字符串
string s = "hello" ;
s.push_back('!') ; //将'!'追加字符到字符串s中 ③. insert - 插入
string s = "hello" ;
s.insert(2, "www") ; //将字符串"www"插入到字符串s中, 插入位置为2 ④. erase - 从字符串中擦除一些字符
string s = "hello" ;
s.erase(1, 2) ; //从下标为1处向后擦去2个字符 ⑤. swap - 与另一字符串交换内容
string s1 = "hello" ;
string s2 = "world" ;
s1.swap(s2) ; //将s1与s2中的字符串进行交换

 

九、获取字符串状态

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

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

#include <iostream>
#include <string> using namespace std; //"12212"这个字符串的所有"12"都替换成"21"
string& replace_all( string str, const string& old_value, const string& new_value ) //替换为22211
{
while( true )
{
string::size_type pos( );
if( ( pos = str.find( old_value ) ) != string::npos )
{
str.replace( pos, old_value.length(), new_value );
}
else
{
break;
}
}
return str;
} string& replace_all_distinct( string str, const string& old_value, const string& new_value ) //替换为21221
{
for( string::size_type pos( ); pos != string::npos; pos += new_value.length() )
{
if( ( pos = str.find( old_value, pos ) ) != string::npos )
{
str.replace( pos, old_value.length(), new_value );
}
else
{
break;
}
}
return str;
} int main()
{
cout << replace_all( string(""), "", "" ) << endl; //22211
cout << replace_all_distinct( string(""), "", "" ) << endl; //21221
}

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. CAN总线基础知识(一)

    1.CAN总线是什么? CAN(Controller Area Network)是ISO国际标准化的串行通信协议.广泛应用于汽车.船舶等.具有已经被大家认可的高性能和可靠性. CAN控制器通过组成总线 ...

  2. [2015-06-10 20:53:50 - Android SDK] Error when loading the SDK:

    1.错误描述 [2015-06-10 20:53:50 - Android SDK] Error when loading the SDK: Error: Error parsing D:\Andro ...

  3. VxWorks启动过程详解(下)

    上一节主要是从映像的分类和各种映像的大致加载流程上看VxWorks的启动过程,这一节让我们从函数级看一下VxWorks的启动过程: 1. Boot Image + Loadable Images: 下 ...

  4. Java中集合List,Map和Set的区别

    Java中集合List,Map和Set的区别 1.List和Set的父接口是Collection,而Map不是 2.List中的元素是有序的,可以重复的 3.Map是Key-Value映射关系,且Ke ...

  5. .Net之路,感谢对我深远影响的三位前辈

    为什么要写这篇文章?为什么创立这个站点? 本人大四,学习.Net三年有余,随着近期开始转向对.Net Core的关注,我开始了解到了张善友(www.csharpkit.com).腾飞(www.jess ...

  6. MFC与Webbrower交互(通过JS)

    最近修改老旧的MFC项目,用的网页做界面,和HTML交互采用的是COM方式,繁琐,丑陋又性能低,于是考虑利用js来进行界面交互,查了一天的资料,现在整理如下,供后来需要的人参考,虽然大概几乎不会有人用 ...

  7. sqoop将mysql连表查询结果导入hdfs文件

    sqoop import --connect jdbc:mysql://ip/数据库 --username 用户名 --password 密码 --query " select p.bidN ...

  8. each遍历的用法

  9. Android继承AppCompatActivity实现全屏设置

    转载 2016年05月25日 13:20:25 标签: android / style / android studio / 继承 4839 Android studio创建新项目后Activity默 ...

  10. es6学习笔记--解构赋值

    昨天学习了es6语法中解构赋值,解构赋值在声明中和函数传参提高了灵活性和便捷性,值得掌握该语法. 概念: ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构.   数组的解构 ...