用另一个子字符串 C++ 替换子字符串 [英] Replace substring with another substring C++

查看:27
本文介绍了用另一个子字符串 C++ 替换子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用 C++ 中的另一个子字符串替换字符串中的一个子字符串,我可以使用哪些函数?

How could I replace a substring in a string with another substring in C++, what functions could I use?

eg: string test = "abc def abc def";
test.replace("abc", "hij").replace("def", "klm"); //replace occurrence of abc and def with other substring

推荐答案

,你可以使用 std::regex_replace:

In c++11, you can use std::regex_replace:

#include <string>
#include <regex>

std::string test = "abc def abc def";
test = std::regex_replace(test, std::regex("def"), "klm"); // replace 'def' -> 'klm'
// test = "abc klm abc klm"

这篇关于用另一个子字符串 C++ 替换子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆