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, 2); //"llo World" 将s1的第2个位置到末尾当作字符串的初值 string s5(s1, 3, 5); //"lo Wo" 将s1的第2个位置开始的5个字符作为字符串的初值 string s8(5, '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", 10) ; // -1 从第10个位置开始找w s1.find_first_of("o"); //6 找第1次出现"o"的位置 s1.find_first_of("o",7); //8 从s1的第7个位置开始查找 s1.find_first_not_of("h"); //3 找第1个不是"h"的字符的位置 s1.find_last_of("o"); //8 //从后面开始找"o" s1.find_last_of("o", 7); //6 //从后面7个字符中找"o"出现的位置 s1.find_last_not_of("d"); //10
七、替换和字串
string s1,s2,s3; s1 = "hello world!" ; s2 = s1.substr(3, 5); //"lo wo" 从第3个位置开始,往后5个字符 s3 = s1.substr(6); //"world!" 从第6个字符到末尾部分 s1.replace(2, 5, "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( 0 );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( 0 ); 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("12212"), "12", "21" ) << endl; //22211cout << replace_all_distinct( string("12212"), "12", "21" ) << endl; //21221 }
转载于:https://www.cnblogs.com/jeakeven/p/4559029.html
STL -- string类字符串相关推荐
- C++ 笔记(22)— STL string 类(字符串赋值、访问、拼接、查找、翻转、大小写转换)
1. 实例化和赋值 STL string #include <string> #include <iostream>int main () {using namespace s ...
- hiho1482出勤记录II(string类字符串中查找字符串,库函数的应用)
string类中有很多好用的函数,这里介绍在string类字符串中查找字符串的函数. string类字符串中查找字符串一般可以用: 1.s.find(s1)函数,从前往后查找与目标字符串匹配的第一个位 ...
- C++之string类字符串连接
概括:对于string类变量,我们可以直接用"+"或者"+="进行字符串的连接.用"+"进行字符串连接时,操作符左右两边既可以都是strin ...
- java字符串比较配置_Java String类字符串比较
Java String类字符串比较 以下实例中我们通过字符串函数 compareTo (string) ,compareToIgnoreCase(String) 及 compareTo(object ...
- c++ string类_C++|细说STL string类概貌及底层细节
C语言中的字符串称为C风格字符串,是一个以'0'结尾的字符数组,string.h库只提供了有限.不甚安全的字符串操作函数.char str[]只能定义编译期确定大小的字符串,而保存在堆内存的动态字符数 ...
- java实用教程——常用实用类——String类(字符串类)
JAVA把String类定义为final类(因此用户不能扩展String类,即String类不可以有子类) String对象可以用"+"进行并置运算 identityHashCod ...
- Java中,String类字符串拼接 用concat方法 和直接用“+”连接符拼接的区别
在String类中,字符串拼接既可以使用concat方法,也可以直接用连接符进行连接,那么两者有什么相同点和不同点呢,下面小编带大家通过代码一起来看一下. concat方法的使用:public Str ...
- String类字符串习题作业
编写程序将 "jdk" 全部变为大写,并输出到屏幕,截取子串"DK" 并输出到屏幕 public class Test4 {public static void ...
- Java基础String类-字符串反转
定义一个方法,实现字符串反转 键盘录入一个字符串,调用该方法后,在控制台输出结果 例如,键盘录入abc,输出结果cba 示例代码: public class HomeworkTest03 {publi ...
- c++ string类字符串查找
1) find() 函数 find() 函数用于在 string 字符串中查找子字符串出现的位置,它其中的两种原型为: size_t find (const string& str, size ...
最新文章
- 用过美德乐吸奶器的宝妈们感觉比国产吸奶器怎么样啊?
- PHP与Java使用des加密通讯
- 老王学java之This()的用法
- 第七章 线性回归预测模型
- 【weblogic】部署jfinal编写的应用
- Spark Executor解析
- mysql事务中宕机_事务执行一半,业务系统宕机,数据库的数据会怎样?
- EasyEarth三维可视化解决方案——智慧园区
- ODOO从哪里开始??OpenERP的第一根线头儿
- C++实现【启发式搜索】算法
- python版CSDN博客备份工具
- 战斗在 VUCA 时代
- 2018 年的第一次福利
- 6大热门开源自动化测试框架【建议收藏】
- Prometheus -Grafana部署及部署告警
- win11电脑所有的浏览器都上不了网怎么解决
- Spring 事务管理
- 【随堂笔记】数据结构基础
- 如何写一个魔方二维动态还原MATLAB仿真程序
- APP被工信部下架了怎么办?重新上架流程分享
热门文章
- python 日期操作【转】
- anaconda下载 (清华镜像)
- 10个最常见的 HTML5 面试题及答案
- Out of resources when opening file './xxx.MYD' (Errcode: 24)
- BZOJ-1055 玩具取名
- mac下cocos2dx(带jsoncpp第三方库)编译为android项目心得
- rnqoj-28-合唱队形-最长上升子序列
- Ionic项目中如何使用Native Camera
- 读完了csapp(中文名:深入理解计算机系统)
- 2.Kong入门与实战 基于Nginx和OpenResty的云原生微服务网关 --- Kong 的安装和基本概念