STL --> string类字符串
基本使用方法
一、输入
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类字符串的更多相关文章
- C++标准模板库Stand Template Library(STL)简介与STL string类
参考<21天学通C++>第15和16章节,在对宏和模板学习之后,开启对C++实现的标准模板类STL进行简介,同时介绍简单的string类.虽然前面对于vector.deque.list等进 ...
- hiho1482出勤记录II(string类字符串中查找字符串,库函数的应用)
string类中有很多好用的函数,这里介绍在string类字符串中查找字符串的函数. string类字符串中查找字符串一般可以用: 1.s.find(s1)函数,从前往后查找与目标字符串匹配的第一个位 ...
- 《java入门》第一季之类(String类字符串一旦被赋值就没法改变)
毫无疑问,String类是java里面最重要的类之一.因此它有很多方法需要了解和掌握. 字符串一旦被赋值,值就不能发生改变: package cn.itcast_02; /* * 字符串的特点:一旦被 ...
- 常用类一一字符串相关类一一String类 字符串的使用
Java字符串就是Unicode字符序列,例如“Java”就是4个Unicode字符J,a,v,a组成的. Java没有内置的字符串类型,而是在标准Java类库中提供了一个预定义的类String,每个 ...
- 关于C语言打印string类字符串的问题
首先因为printf函数输出字符串是针对char *的,即printf只能输出c语言的内置数据,而string不是c语言的内置数据. 其次string类型的对象不止包含字符串,还包含了许多用于操作的函 ...
- 01.String类字符串本质
String类是在java开发过程中,使用最最频繁的一个类,不管是 用户名 密码 还是http报文接收过来的数据,其本质就是字符序列 所以做为一个java开发者,我们要重点掌握好String的方法使用 ...
- C++STL—string类
string容器 1.1 string容器的基本概念 string容器是一个类 这个容器中有一个指针,指针维护了一个数组 string容器提供copy.find.insert.replace等等功能 ...
- 萌新笔记——C++里将string类字符串(utf-8编码)分解成单个字(可中英混输)
最近在建词典,使用Trie字典树,需要把字符串分解成单个字.由于传入的字符串中可能包含中文或者英文,它们的字节数并不相同.一开始天真地认为中文就是两个字节,于是很happy地直接判断当前位置的字符的A ...
- String类字符串截取示范
package it.com; // 要求:對字符串“jflksjdfnbalkdfjnbaddddnbahhuhnbauuuuahnbahdfunbadhfudf”进行检索:判断有多少个nba; / ...
随机推荐
- HeapAlloc,GlobalAlloc,LocalAlloc,VirtualAlloc,malloc,new的异同
1. 首先我们来看HeapAlloc: MSDN上的解释为:HeapALloc是从堆上分配一块内存,且分配的内存是不可移动的(即如果没有连续的空间能满足分配的大小,程序不能将其他零散的 空间利用起来, ...
- Windows Subsystem for Linux (WSL)挂载移动硬盘U盘
WSL想通过移动硬盘处理一些数据,结果进去了无法发现移动硬盘,于是搜了好久也没有一个正确的解决办法,终于找到一个,现在贡献出来与大家共享. WSL比起linux挂载硬盘简单一些.而且windows本身 ...
- ROM型启动方式概述
ROM 型启动方式概述 所有的VxWorks 内核映像类型中,只有VxWorks 类型使用的bootrom 引导程序进行启动,此时VxWorks 内核映像放置在主机端,由目标板bootrom 完成Vx ...
- freemarker自定义标签报错(三)
freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Encountered " " at line 14, column ...
- offline页面开发常用方法及页面控件验证
offline页面开发常用方法及页面控件验证,对一些CheckBoxList操作进行封装,新人可以直接使用该代码. 1.返回上一页网址 /// <summary> /// Descript ...
- mac下安装ELK
本文主要为自己所走弯路而做的补充,对小白(比如我)来讲某些博客讲的还是高深了,特地来此补充说明一些东西. 主要步骤参考http://blog.csdn.net/ywheel1989/article/d ...
- doT.js模板引擎及基础原理
时至今日,基于后端JavaScript(Node.js)和MVC思想也开始流行起来.模板引擎是数据和页面分离工作中最重要的一环,在各大门户网站均有利用到模板引擎. 模板引擎有很多种,但是原理了解也是非 ...
- PhotoShop制作简单的文字动画--多媒体技术与应用
下面是最终实现的效果图: 1.新建图像,设置图像属性如下所示. 2.使用[横排文字工具]在背景图像上打上文字内容 3.[图层]——>[图层式样]——>[渐变叠加] 出现“图层样式”面板 4 ...
- 已在Terminal安装了包,PyCharm却提示无法找到
想使用python的dpkt包解析pcap文件进行分析,已经按照提示在终端输入sudo apt install python-dpkt,并且显示安装成功. 但是回到PyCharm中依然报错,如图所示: ...
- 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)
[SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...