Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- Clojure
- C++
- C#
- Dart
- Dart
- Erlang
- Fortran
- Go
- Go
- Haskell
- JS
- Java
- Java
- Java
- Pascal
- Perl
- Python
- Python
- Python
- Ruby
- Rust
(replace {"foo" "bar"} x)
std::replace(x.begin(), x.end(), "foo", "bar");
int i;
while ((i = x.IndexOf("foo")) != -1)
x[i] = "bar";
While "foo" is found in the list
it is replaced by "bar"
This takes quadratic time instead of linear
it is replaced by "bar"
This takes quadratic time instead of linear
for (var i = 0; i < x.length; i++) {
if (x[i] == 'foo') x[i] = 'bar';
}
[case Elem of "foo" -> "bar"; _ -> Elem end || Elem <- X].
where (x == "foo")
x = "bar"
endwhere
func replaceAll[T comparable](s []T, old, new T) {
for i, v := range s {
if v == old {
s[i] = new
}
}
}
replaceAll(x, "foo", "bar")
The type parameter T has a constraint: it must be comparable with ==
for i, v := range x {
if v == "foo" {
x[i] = "bar"
}
}
replaced = map (\e -> if e == "foo" then "bar" else e) x
Variables are not mutable, so we cannot assign it back to x.
x = x.map(e => e === 'foo' ? 'bar' : e);
ListIterator<String> i = x.listIterator();
while (i.hasNext())
if (i.next().equals("foo")) i.set("bar");
x.replaceAll(e -> e.equals("foo") ? "bar" : e);
x = x.stream()
.map(s -> s.equals("foo") ? "bar" : s)
.toList();
map { s/^foo$/bar/g } @x;
for i, v in enumerate(x):
if v == 'foo': x[i] = 'bar'
x.map!{|el| el == "foo" ? "bar" : el}
for v in &mut x {
if *v == "foo" {
*v = "bar";
}
}