基本使用方法

一、输入

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. Java中private、protected和public作用域的异同

    Java中private.protected和public作用域的异同 说明:(1)private的作用范围为当前类,protected的作用范围哦不能超过其他包: (2)区别不同的作用域的不同作用范 ...

  2. HighCharts中的Ajax请求的2D折线图

    HighCharts中的Ajax请求的2D折线图 设计源码: <!DOCTYPE html> <html> <head> <meta charset=&quo ...

  3. 部署Java Web项目报错(二)

    在编写HighCharts折线时,并且数据源是请求CSV,运行项目时出现错误 Uncaught TypeError: Cannot read property 'prototype' of undef ...

  4. Netty的并发编程实践3:CAS指令和原子类

    互斥同步最主要的问题就是进行线程阻塞和唤醒所带来的性能的额外损耗,因此这种同步被称为阻塞同步,它属于一种悲观的并发策略,我们称之为悲观锁.随着硬件和操作系统指令集的发展和优化,产生了非阻塞同步,被称为 ...

  5. directX显示采集源(摄像头)filter

    IGraphBuilder * g_pGraphBuilder = NULL;IBaseFilter *Pbf=0;IVideoWindow  * g_pVWindow = NULL;IMediaCo ...

  6. C#抽象类应用实例

    abstract修饰符可以和类.方法.属性.索引器及事件一起使用,在类声明中使用abstract修饰符以表明这个类只能是其他类的基类. 抽象类的特性 (1)抽象类不能被实例化 (2)抽象类可以包含抽象 ...

  7. Mac 常用快捷键

    可以按下组合键来实现通常需要鼠标.触控板或其他输入设备才能完成的操作. 要使用键盘快捷键,需按住一个或多个修饰键,同时按快捷键的最后一个键.例如,要使用快捷键 Command-C(拷贝),请按住 Co ...

  8. C#技术点--修改系统时间

    C#的System.DateTime类提供了对日期时间的封装,用它进行时间的转换和处理很方便,但是我没有在其中找到任何可以用来修改系统时间的成员.用过VC.VB等的朋友可能知道,我们可以调用Win32 ...

  9. Idea工具开发 SpringBoot整合JSP(毕设亲测可用)

    因为,临近毕业了,自己虽然也学了很多框架.但是,都是在别人搭建好的基础上进行项目开发.但是springboot的官方文档上明确指出不提倡使用jsp进行前端开发,但是在校期间只学了jsp作为前端页面.所 ...

  10. 细说css中的position属性

    有过css开发经验的同学,对于position这个属性一定不会陌生,然而这个熟悉的属性确是面试题中的常客,也就说明了该属性在css的世界是有一定的江湖地位的,那么我们就来详细的说说position这个 ...