This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
- C++
- C++
- C#
- D
- Dart
- Elixir
- Erlang
- Fortran
- Go
- Go
- Haskell
- JS
- JS
- Java
- Lua
- PHP
- Pascal
- Perl
- Python
- Ruby
- Rust
- VB
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;
}
constexpr version which doesn't depend on regex
auto x2 = x.replace(y,z);
Same as
auto x2 = replace(x,y,z)
but easier to guess operation in this form.
auto x2 = replace(x,y,z)
but easier to guess operation in this form.
character(len=:), allocatable :: x
character(len=:), allocatable :: x2
character(len=:), allocatable :: y,z
integer :: j, k
k=1
do
j = index(x(k:),y)
if (j==0) then
x2 = x2 // x(k:)
exit
end if
if (j>1) then
x2 = x2 // x(k:j+k-2)
end if
x2 = x2 // z
k = k + j + len(y) - 1
end do
This code uses reallocation on assignment for character variables. An alternative implementation might count bytes first, then allocate, then do this.
Note that _// is string concatenation in Fortran.
Note that _// is string concatenation in Fortran.