替换string中的部分字符
生活随笔
收集整理的這篇文章主要介紹了
替换string中的部分字符
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
功能
向函數fun中傳入三個參數:將s中所有oldval替換為newval
代碼
#include <iostream> #include <list> #include <deque> #include <vector> #include <forward_list> #include <array> using namespace std;void fun(string &s, const string &oldval, const string &newval){int p = 0; // 匹配成功位置的下標while ((p = s.find(oldval, p)) != -1) {// 遍歷s以查找oldvals.replace(p, oldval.size(), newval);// 找到以后替換為newvalp += newval.size();// 調整下標,略過替換的newval} }int main(int argc, char const *argv[]) {string s = "hello tho thru th thr tho thru";fun(s, "thog", "though");fun(s, "thru", "through");cout << s << endl;return 0; } 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的替换string中的部分字符的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 民生信用卡超限功能怎么开通?这种方法也能
- 下一篇: java 监听器 分类_java过滤器和