Logo

Programming-Idioms

History of Idiom 63 > diff from v17 to v18

Edit summary for version 18 by :

Version 17

2015-09-05, 10:30:11

Version 18

2015-10-29, 14:05:15

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.

Imports
import Data.List
Imports
import Data.List
Code
allchanged [] _ _ = []
allchanged input from to = if isPrefixOf from input
  then to ++ allchanged (drop (length from) input) from to
  else head input : allchanged (tail input) from to

x2 = allchanged x y z
Code
allchanged [] _ _ = []
allchanged input from to = if isPrefixOf from input
  then to ++ allchanged (drop (length from) input) from to
  else head input : allchanged (tail input) from to

x2 = allchanged x y z
Imports
import std.stdio;
import std.array;
Imports
import std.stdio;
import std.array;
Code
void main()
{
	auto x = "111122223333";
	auto y = "22";
	auto z = "555";

	auto x2 = x.replace(y,z);  

        writeln(x2);

}
Code
void main()
{
	auto x = "111122223333";
	auto y = "22";
	auto z = "555";

	auto x2 = x.replace(y,z);  

        writeln(x2);

}
Comments bubble
same as
auto x2 = replace(x,y,z)
but easier to guess operation in this form
Comments bubble
same as
auto x2 = replace(x,y,z)
but easier to guess operation in this form
Doc URL
http://dlang.org/phobos/std_array.html#.replace
Doc URL
http://dlang.org/phobos/std_array.html#.replace
Code
let x2 = x.replace(&y, &z);
Code
let x2 = x.replace(&y, &z);
Comments bubble
Returns a string slice (`&str`). Add `.to_string()` or `.to_owned()` to get a `String`.
Comments bubble
Returns a string slice (`&str`). Add `.to_string()` or `.to_owned()` to get a `String`.
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20let%20x%20%3D%20%22lorem%20ipsum%20dolor%20lorem%20ipsum%22%3B%0A%20%20%20%20let%20y%20%3D%20%22lorem%22%3B%0A%20%20%20%20let%20z%20%3D%20%22LOREM%22%3B%0A%0A%20%20%20%20let%20x2%20%3D%20x.replace(%26y%2C%20%26z)%3B%0A%20%20%20%20%0A%20%20%20%20println!(%22%7B%7D%22%2C%20x2)%3B%0A%7D%0A&version=stable
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20let%20x%20%3D%20%22lorem%20ipsum%20dolor%20lorem%20ipsum%22%3B%0A%20%20%20%20let%20y%20%3D%20%22lorem%22%3B%0A%20%20%20%20let%20z%20%3D%20%22LOREM%22%3B%0A%0A%20%20%20%20let%20x2%20%3D%20x.replace(%26y%2C%20%26z)%3B%0A%20%20%20%20%0A%20%20%20%20println!(%22%7B%7D%22%2C%20x2)%3B%0A%7D%0A&version=stable
Imports
import "strings"
Imports
import "strings"
Code
x2 := strings.Replace(x, y, z, -1)
Code
x2 := strings.Replace(x, y, z, -1)
Comments bubble
-1 means "no limit on the number of replacements".
This replaces non-overlapping instances of y.
Comments bubble
-1 means "no limit on the number of replacements".
This replaces non-overlapping instances of y.
Doc URL
http://golang.org/pkg/strings/#Replace
Doc URL
http://golang.org/pkg/strings/#Replace
Demo URL
https://play.golang.org/p/UV-04jpLY8
Demo URL
https://play.golang.org/p/UV-04jpLY8