鱼C论坛

 找回密码
 立即注册
查看: 2971|回复: 3

如何替换字符串中指定的一部分?

[复制链接]
发表于 2019-3-18 01:04:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <iostream>
#include <string>
using namespace std;
int main()
{
        string a = "|,ISBN4587,,时间简史,,史蒂芬·霍金,,出版社,,56,||,AD45YT32,,全球通史,,斯塔夫里阿诺斯,,出版社,,69,||,666666666,,C++从入门到放弃,,杨永信,,出版社,,79,||,CB10753D,,魔法禁书目录,,镰池和马,,出版社,,88,||,1141WERT,,双城记,,查尔斯·狄更斯,,出版社,,23,|";
        int H = a.find("霍金");
        int C = a.rfind('|', H);
        int M = a.find('|', H);
        cout << a.replace(C, H + 1, "hahhahahahahhahahah");
        system("pause");
}

例如上面一段代码,字符串a中包括了几本书的相关信息,每一本用两个'|'包了起来。我想用“hahahhahahahhaha”替换第一个用'|'字符包着的字符段,所以想的是先找到其中某个子串的位置,然后通过find和rfind找到前后两个'|'的索引,再通过replace替换。但事实上却行不通,“hahahahhaha”总是无法准确替换我想替换的第一个'|'段,即使我想去替换其他的'|'段也是一样,要么是多替换了,要么就是少替换,有时候还会出现乱码一样的东西。请问这是为什么?以及,该如何改正?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-3-18 07:21:50 | 显示全部楼层
莫有人吗······
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-18 11:29:15 | 显示全部楼层
string类的替换函数:
string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符
string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const string &s, int pos, int n);//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符
string &replace(int p0, int n0,int n, char c);//删除p0开始的n0个字符,然后在p0处插入n个字符c
string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之间的部分替换为字符串s
string &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之间的部分替换为s的前n个字符
string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之间的部分替换为串s
string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之间的部分替换为n个字符c
string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之间的部分替换成[first,last)之间的字符串


string类的插入函数:
string &insert(int p0, const char *s);
string &insert(int p0, const char *s, int n);
string &insert(int p0,const string &s);
string &insert(int p0,const string &s, int pos, int n);
//前4个函数在p0位置插入字符串s中pos开始的前n个字符
string &insert(int p0, int n, char c);//此函数在p0处插入n个字符c
iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字符
void insert(iterator it, int n, char c);//在it处插入n个字符c


string类的删除函数
iterator erase(iterator first, iterator last);//删除[first,last)之间的所有字符,返回删除后迭代器的位置
iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置
string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-18 11:44:28 | 显示全部楼层
82457097 发表于 2019-3-18 11:29
string类的替换函数:
string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后 ...

干的漂亮!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-3 12:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表