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[ ...
随机推荐
- 嵌入式(Embedded System)笔记 —— Cortex-M3 Introduction and Basics(下)
随着课内的学习,我想把每节课所学记录下来,以作查阅.以饲读者.由于我所上的是英文班课程,因此我将把关键术语的英文给出,甚至有些内容直接使用英文. 本次所介绍内容仍是关于Cortex-M3的基础内容,相 ...
- python学习笔记一:数据类型
一.Python文件类型 1.源代码 hello.py: 1 #!/usr/bin/python 2 print "hello world" 2.字节代码:python源文件经编译 ...
- appium 多个设备同时执行
测试需要同时在多个android设备上运行,就需要启动多个appium 使用adb命令获取udid,命令:adb get-serialno 使用的是testng测试框架,代码使用java编写 第一台, ...
- 孤荷凌寒自学python第十六天python的迭代对象
孤荷凌寒自学python第十六天python的迭代对象 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 迭代也就是循环. python中的迭代对象有相关的如下几个术语: A容器 contrai ...
- sdram之乒乓操作
在实时显示时,为了保证画面显示的完整性需要对SDRAM进行乒乓操作. SDRAM 中有 4 个bank ,地址分别为00 01 10 11,后面将用 0 1 2 3来描述 bank 0和1 作为第一个 ...
- HDU 3954 Level up (线段树特殊懒惰标记)
http://blog.csdn.net/acm_cxlove/article/details/7548087 感觉最巧的是定义了min_dis……将区间内有无英雄升级分开处理 #include &l ...
- php记日志
就是把log追加到文件中 用到了一个方法 file_put_contents <?php file_put_contents('a',date('Y m d h:i:s').' some tex ...
- CSLM 配置粗解
CSLM工具(continuous space language model toolkit)用于训练NNLM,支持SRILM.KENLM(默认)语言模型工具,CUDA加速,CSTM统计机器翻译. 本 ...
- P1194 买礼物
题目描述 又到了一年一度的明明生日了,明明想要买B样东西,巧的是,这B样东西价格都是A元. 但是,商店老板说最近有促销活动,也就是: 如果你买了第I样东西,再买第J样,那么就可以只花K[I,J]元,更 ...
- [2018-9-4T2]探索黑暗dark
题目大意:有一棵树,第$i$个点的点权为$s_i(s_1>0)$.初始有每个点都是亮的.$m$次修改,每次改变一个点的亮暗,回答包含$1$的全亮的连通块中点权和最大的连通块的和的值. 题解:正解 ...