/*
用法一:
用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的更多相关文章

  1. nyoj 113 字符串替换 (string中替换函数replace()和查找函数find())

    字符串替换 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 编写一个程序实现将字符串中的所有"you"替换成"we"   输入 ...

  2. [LeetCode] Find And Replace in String 在字符串中查找和替换

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  3. [Swift]LeetCode833. 字符串中的查找与替换 | Find And Replace in String

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  4. 833. Find And Replace in String

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  5. Leetcode: Find And Replace in String

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  6. LC 833. Find And Replace in String

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  7. 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 ...

  8. 【LeetCode】833. Find And Replace in String 解题报告(Python)

    [LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  9. C# Byte[] 转String 无损转换

    C# Byte[] 转String 无损转换 转载请注明出处 http://www.cnblogs.com/Huerye/ /// <summary> /// string 转成byte[ ...

随机推荐

  1. 初学JS——利用JS制作的别踩白块儿(街机模式) 小游戏

    这个是上个星期5写的了,当时是突然想写个游戏,就想到了别踩白块儿,当时的想法是 可能普通模式的别踩白块儿因为他的“块儿”是滚动的向上这种,以我目前会的技术想不出怎么写, 但是如果是街机模式,通过你每按 ...

  2. Python之日志处理 logging模块

    Python之日志处理(logging模块)   本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 logging模块日志流处理流程 使用logging四 ...

  3. [译]如何去除pandas dataframe里面的Unnamed的列?

    原文来源: https://stackoverflow.com/questions/43983622/remove-unnamed-columns-in-pandas-dataframe 问:我有一个 ...

  4. Scala 基础(5)—— 构建函数式对象

    有了 Scala 基础(4)—— 类和对象 的前提,现在就可以来构建一个基于 Scala 的函数式对象. 下面开始构造一个有理数对象 Rational. 1. 主构造方法和辅助构造方法 对于每一个类的 ...

  5. 【bzoj4974】字符串大师 逆模拟KMP

    题目描述 一个串T是S的循环节,当且仅当存在正整数k,使得S是$T^k$(即T重复k次)的前缀,比如abcd是abcdabcdab的循环节.给定一个长度为n的仅由小写字符构成的字符串S,请对于每个k( ...

  6. 【bzoj1856】[Scoi2010]字符串 Catalan数

    题目描述 lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数不能少于0的个数.现在lxhgww想要知道满足 ...

  7. 雅礼集训 Day2 T3 联盟 解题报告

    联盟 题目描述 \(\text{G}\) 国周边的 \(n\) 个小国家构成一个联盟以抵御 \(\text{G}\) 国入侵, 为互相支援,他们建立了\(n−1\) 条双向通路, 使得任意两个国家可以 ...

  8. EAR、JAR、WAR(IT)

    EAR文件包括整个项目,内含多个ejb module(jar文件)和web module(war文件)   JAR.WAR.EAR.在文件结构上,三者并没有什么不同,它们都采用zip或jar档案文件压 ...

  9. Topcoder SRM 603 div1题解

    昨天刚打了一场codeforces...困死了...不过赶在睡前终于做完了- 话说这好像是我第一次做250-500-1000的标配耶--- Easy(250pts): 题目大意:有一棵树,一共n个节点 ...

  10. WCF技术剖析 Two

    WCF终结点和寻址之--AddressHead信息匹配代码 Contracts契约 using System; using System.Collections.Generic; using Syst ...