/*
用法一:
用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. 找出Xcode没有使用的图片

    #! /bin/sh PROJ=`find . -name '*.xib' -o -name '*.[mh]'` for png in ` find . -name '*.png'` do name ...

  2. 《Cracking the Coding Interview》——第18章:难题——题目9

    2014-04-29 04:18 题目:有一连串的数被读入,设计一个数据结构,能随时返回当前所有数的中位数. 解法:用一个大顶堆,一个小顶堆将数分成数量最接近的两份,就能轻松得到中位数了. 代码: / ...

  3. 《数据结构》C++代码 堆(优先队列)

    堆,是优先队列最常用的一种实现方式.在优先队列中,每个元素都被赋予了一个优先级,而每次出队时都让优先级最高的元素出队.堆,则是一种存储优先队列的方法,特指以一棵树形式存储的优先队列.最常用的是二叉堆, ...

  4. 转:Redis设置认证密码 Redis使用认证密码登录 在Redis集群中使用认证密码

    Redis默认配置是不需要密码认证的,也就是说只要连接的Redis服务器的host和port正确,就可以连接使用.这在安全性上会有一定的问题,所以需要启用Redis的认证密码,增加Redis服务器的安 ...

  5. python基础实践(二)

    -*-越简单越快乐-*-# -*- coding:utf-8 -*-# Author:sweeping-monkQuestion_1 = "python中的整数运算"Method_ ...

  6. winform-windowsmediaplayer设置可视化效果之条形

    winform导入windowsmediaplayer这个COM组件,他的默认可视化效果为: 而我们需要的可视化效果为: 则我们可以通过代码更改可视化效果:(参数value设为4即可!) //设置可视 ...

  7. Hadoop平台K-Means聚类算法分布式实现+MapReduce通俗讲解

        Hadoop平台K-Means聚类算法分布式实现+MapReduce通俗讲解 在Hadoop分布式环境下实现K-Means聚类算法的伪代码如下: 输入:参数0--存储样本数据的文本文件inpu ...

  8. poj 2299 归并排序求逆序数 (可做模板)

    Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 48077   Accepted: 17533 Description In ...

  9. Java分布式数据导出实践

    伴随业务发展日益剧增,对数据的要求越来越多也越来越高. 用户在浏览器发起导出请求--web服务器接收请求--请求后台获取数据--数据统计后生成excel或其他图标--响应给客户端 整个过程至少5步,才 ...

  10. BZOJ2599 [IOI2011]Race 【点分治】

    题目 给一棵树,每条边有权.求一条简单路径,权值和等于K,且边的数量最小.N <= 200000, K <= 1000000 输入格式 第一行 两个整数 n, k 第二..n行 每行三个整 ...