History of Idiom 63 > diff from v58 to v59
Edit summary for version 59 by dstbstr:
New C++ implementation by user [dstbstr]
New C++ implementation by user [dstbstr]
↷
Version 58
2022-02-17, 12:10:21
Version 59
2024-04-29, 23:49:38
Idiom #63 Replace fragment of a string
Assign to x2 the value of string x with all occurrences of y replaced by z.
Assume occurrences of y are not overlapping.
Idiom #63 Replace fragment of a string
Assign to x2 the value of string x with all occurrences of y replaced by z.
Assume occurrences of y are not overlapping.
Variables
x2,x,y,zVariables
x2,x,y,zExtra Keywords
substring substituteExtra Keywords
substring substituteCode
constexpr std::string ReplaceAllCopy(const std::string& x, const std::string& y, const std::string& z) {
auto x2 = x;
size_t i = 0;
while (true) {
i = x2.find(y, i);
if (i == std::string::npos) break;
x2.replace(i, y.length(), z);
}
return x2;
}
static_assert(ReplaceAllCopy("AbAbAb", "b", "dd") == "AddAddAdd");
static_assert(ReplaceAllCopy("AbcAbc", "bc", "d") == "AdAd");
Comments bubble