replace函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
用正常方法
#include <iostream>
#include <string>
using namespace std;
int main() {
    string Oldstr
    string str = "我是渣渣辉,我上八十年级";
    cout<<str<<endl;
    str = str.replace(str.find("渣"),6,"王一行");  //从第一个(渣)的位置开始的三个汉字替换成(王一行)
    str = str.replace(str.find("八"),4,"五");  //从第一个(八)的位置开始的两个汉字替换成(五)
    //replace函数的
    cout<<str;
    return 0;
}
 
用begin
#include <iostream>
#include <string>
using namespace std;
int main() {
    string str = "he is@ a@ good boy";
    //cout<<str<<endl;
    str = str.replace(str.begin(),str.begin()+5,"#");  //从#替换从begin位置开始的5个字符
    cout<<str<<endl;
    return 0;
}
 
用指针
#include <iostream>
#include <string>
using namespace std;
int main() {
    string str = "he is@ a@ good boy";
    char *str1 = "12345";
    str = str.replace(0,5,str1);  //从#替换从begin位置开始的5个字符
    cout<<str<<endl;
    return 0;
}*/
 
 
#include <iostream>
#include <string>
using namespace std;
int main() {
    string str = "he is@ a@ good boy";
    char *str1 = "12345";
    str = str.replace(0,6,str1,4);  //用str1替换从begin位置开始的5个字符
    cout<<str<<endl;
    return 0;
}

  

posted @   王一行  阅读(90)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示