string那些事之replace
/*
用法一:
用str替换指定字符串从起始位置pos开始 长度为为len的字符串
string &replace(size_t pos, size_t len, const string& str)
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double eps = 1e-7;
const int maxn = 5e5 + 5;
const double pi = acos(-1.0);
int main()
{
string s="this is@ a test string!";
s=s.replace(s.find("@"),1,"");
cout<<s<<endl;
}
//this is a test string!
/*
用法二:
用str替换 迭代器 起始位置 和 结束位置的字符串
string &replace(const_iterator i1, const_iterator i2, const string& str)
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double eps = 1e-7;
const int maxn = 5e5 + 5;
const double pi = acos(-1.0);
int main()
{
string s="this is@ a@ test string!";
s=s.replace(s.begin(),s.begin()+6,"");
cout<<s<<endl;
}
//s@ a@ test string!
/*
用法三:
用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串
string &replace(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen)
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double eps = 1e-7;
const int maxn = 5e5 + 5;
const double pi = acos(-1.0);
int main()
{
string s="this is@ a@ test string!";
string str="12345";
s=s.replace(0,5,str,str.find("1"),3); //用str的指定子串(从1位置数共3个字符)替换从0到5位置上的s
cout<<s<<endl;
}
//123is@ a@ test string!
/*
用法四:**string转char*时编译器可能会报出警告,不建议这样做**
用str替换从指定位置0开始长度为5的字符串
string &replace(size_t pos, size_t len, const char*s)
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double eps = 1e-7;
const int maxn = 5e5 + 5;
const double pi = acos(-1.0);
int main()
{
string s="this is@ a@ test string!";
char* str="12345";
s=s.replace(0,5,str); //用str替换从指定位置0开始长度为5的字符串
cout<<s<<endl;
}
//12345is@ a@ test string!
/*
用法五:**string转char*时编译器可能会报出警告,不建议这样做**
用str替换从指定迭代器位置的字符串
string &replace(const_iterator i1, const_iterator i2, const char* s)
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double eps = 1e-7;
const int maxn = 5e5 + 5;
const double pi = acos(-1.0);
int main()
{
string s="this is@ a@ test string!";
char* str="12345";
s=s.replace(s.begin(),s.begin()+9,str); //用str替换从指定迭代器位置的字符串
cout<<s<<endl;
}
//12345a@ test string!
/*
用法六:**string转char*时编译器可能会报出警告,不建议这样做**
用s的前n个字符替换从开始位置pos长度为len的字符串
string& replace(size_t pos, size_t len, const char* s, size_t n)
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double eps = 1e-7;
const int maxn = 5e5 + 5;
const double pi = acos(-1.0);
int main()
{
string s="this is@ a@ test string!";
char* str="12345";
s=s.replace(0,9,str,4); //用str的前4个字符替换从0位置开始长度为9的字符串
cout<<s<<endl;
}
//1234a@ test string!
/*用法七:string转char*时编译器可能会报出警告,不建议这样做
用s的前n个字符替换指定迭代器位置(从i1到i2)的字符串
string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);
*/
int main()
{
string line = "this@ is@ a test string!";
char* str = "12345";
line = line.replace(line.begin(), line.begin()+9, str, 4); //用str的前4个字符替换指定迭代器位置的字符串
cout << line << endl;
return 0;
}
/*
用法八:用重复n次的c字符替换从指定位置pos长度为len的内容
string &replace(size_t pos, size_t len, size_t, char c)
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double eps = 1e-7;
const int maxn = 5e5 + 5;
const double pi = acos(-1.0);
int main()
{
string s="this is@ a@ test string!";
char c='z';
s=s.replace(0,9,3,c); //用重复3次的c字符替换从指定位置0长度为9的内容
cout<<s<<endl;
}
//zzza@ test string!
/*
用法九:用重复n次的c字符替换从指定迭代器位置的内容
string &replace(const_iterator i1, const_iterator i2, size_t, char c)
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double eps = 1e-7;
const int maxn = 5e5 + 5;
const double pi = acos(-1.0);
int main()
{
string s="this is@ a@ test string!";
char c='z';
s=s.replace(s.begin(),s.begin()+9,3,c); //用重复3次的c字符替换从指定位置0长度为9的内容
cout<<s<<endl;
}
//zzza@ test string!
string那些事之replace的更多相关文章
- nyoj 113 字符串替换 (string中替换函数replace()和查找函数find())
字符串替换 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 编写一个程序实现将字符串中的所有"you"替换成"we" 输入 ...
- [LeetCode] Find And Replace in String 在字符串中查找和替换
To some string S, we will perform some replacement operations that replace groups of letters with ne ...
- [Swift]LeetCode833. 字符串中的查找与替换 | Find And Replace in String
To some string S, we will perform some replacement operations that replace groups of letters with ne ...
- 833. Find And Replace in String
To some string S, we will perform some replacement operations that replace groups of letters with ne ...
- Leetcode: Find And Replace in String
To some string S, we will perform some replacement operations that replace groups of letters with ne ...
- LC 833. Find And Replace in String
To some string S, we will perform some replacement operations that replace groups of letters with ne ...
- 833. Find And Replace in String —— weekly contest 84
Find And Replace in String To some string S, we will perform some replacement operations that replac ...
- 【LeetCode】833. Find And Replace in String 解题报告(Python)
[LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- C# Byte[] 转String 无损转换
C# Byte[] 转String 无损转换 转载请注明出处 http://www.cnblogs.com/Huerye/ /// <summary> /// string 转成byte[ ...
随机推荐
- TIDB介绍
TiDB 是什么? TiDB 是一个分布式 NewSQL 数据库.它支持水平弹性扩展.ACID 事务.标准 SQL.MySQL 语法和 MySQL 协议,具有数据强一致的高可用特性,是一个不仅适合 O ...
- 一道简单的CTFpython沙箱逃逸题目
看了几天的ssti注入然后了解到有python沙箱逃逸 学过ssti注入的话python沙箱逃逸还是很容易理解的. 看一道CTF题目,源码的话我改了改,一开始不能用,直接在py2上运行就好. 题目要求 ...
- Python 3基础教程8--if else、if elif else
本文介绍if else语句,不多说,直接看例子. if elif else语句
- Pytest 断言
pytest 断言 断言:一个标准的用例都包含了断言,编写pytest自动化脚本的时候,也需要设置断言 assert使用 常用分三种 1:比较大小与是否相等 2:包含或不包含 3:验证boolean ...
- c++ object model
对一个结构体进行不断的封装后可以形成一个c++类,为此需要添加很多函数成员之类的代码,为此显示c++比c语言显得庞大并且迟缓,但是事实并不是这些 c++在布局和时间上的额外承担主要是由virtual引 ...
- XML快速入门
XML是什么 Extensible Markup Language 自定义标签: 用来传输数据: 可扩展标记语言,是一种类似超文本标记语言的标记语言. 与HTML的比较: 1.不是用来替代HTML的: ...
- shit vue-cli & path bug & baseUrl bug
vue-cli path bug https://cli.vuejs.org/zh/guide/#cli baseUrl bug baseUrl: "././" , https:/ ...
- 【bzoj4870】[Shoi2017]组合数问题 dp+快速幂/矩阵乘法
题目描述 输入 第一行有四个整数 n, p, k, r,所有整数含义见问题描述. 1 ≤ n ≤ 10^9, 0 ≤ r < k ≤ 50, 2 ≤ p ≤ 2^30 − 1 输出 一行一个整数 ...
- ESXI6.0 时间(时区)显示不一致
ESXI6.0 时间(时区)显示不一致 来源 http://blog.51cto.com/jdonghong/1957118 近日由于设置ESXI计划任务,无意间发现了esxi服务器客服端时间和系统显 ...
- 百度AI开放平台 UNIT平台开发在线客服 借助百度的人工智能如何开发一个在线客服系统
这段时间在研究一些人工智能的产品,对比了国内几家做人工智能在线客服的,有些接口是要收费的,有些是免费的,但是做了很多限制,比如每天调用的接口次数限制是100次.后来就找到了百度的AI,大家也知道,目前 ...