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[ ...
随机推荐
- codility
// you can also use imports, for example: // import java.util.*; // you can write to stdout for debu ...
- Android FrameWork 概述
Framework是什么 Framework的中文意思是“框架”,在软件开发中通常指开发框架,在一个系统中处于内核层之上,为顶层应用提供接口,被设计用来帮助开发者快速开发顶层应用,而不必关心系统内核运 ...
- MySQL隐式转换测试
Preface There're various data type in MySQL such as number,string,date,time,lob,etc.The data ...
- 【Python】python常用模块
一.模块.包 什么是模块? 模块实质上就是一个python文件,它是用来组织代码的,意思就是说把python代码写到里面,文件名就是模块的名称,test.py test就是模块名称. 什么是包? 包, ...
- gradle构建
https://blog.csdn.net/baidu_30809315/article/details/77865414
- 【转载】Unity3D研究院之共享材质的巧妙用法(sharedMaterial效率问题)
如果你需要修改模型材质的颜色,或者是修改材质Shader的一些属性, 通常情况是用获取模型的Renderer组件,然后获取它的material属性. 举个简单的例子,修改颜色或者直接更换shader ...
- Python全栈工程师(迭代器、字节串)
ParisGabriel 每天坚持手写 一天一篇 决定坚持几年 为了梦想为了信仰 Python人工智能从入门到精通 迭代器 Iterator: 用<&g ...
- (原) Unreal搬山-引言(图多慎)
@author:白袍小道 扯淡:(图多) 何为搬山,这里借了剑来少年郎一句.(若有同道中人,甚是开心,开心的很) 江湖岂能没前辈) (江湖很大,足够你浪) (刺客信条 \荒野 \神秘海域 \死亡空间 ...
- PAT 1070 结绳
https://pintia.cn/problem-sets/994805260223102976/problems/994805264706813952 给定一段一段的绳子,你需要把它们串成一条绳. ...
- php session 测试
2018-06-22 08:26:30 session指的是默认php提供的文件session形式 当前我的认识是,php并不记录session的过期时间,但是php.ini中有session的垃圾回 ...