(我假設你的地圖有串鑰匙,而不是原子)
如果你的罰款由一個運行的替代品之一,你可以簡單地使用Enum.reduce
與String.replace
:
iex(1)> my_map = %{"old_value1" => "new_value1", "old_value2" => "new_value2"}
%{"old_value1" => "new_value1", "old_value2" => "new_value2"}
iex(2)> str = "some string .......old_value1 fafdsfd old_value2 faaaaaaa"
"some string .......old_value1 fafdsfd old_value2 faaaaaaa"
iex(3)> Enum.reduce(my_map, str, fn {old, new}, str -> String.replace(str, old, new) end)
"some string .......new_value1 fafdsfd new_value2 faaaaaaa"
但是,如果你想並行運行它們,爲了性能或者正確性(如果地圖上的任何鍵匹配任何其他值,以前的解決方案將給出不同的結果),我會使用類似:binary.compile_pattern
,:binary.match
和模式匹配:
defmodule Main do
def replace(string, map) do
replace(string, map, :binary.compile_pattern(Map.keys(map)), "")
end
defp replace(string, map, pattern, acc) do
case :binary.match(string, pattern) do
{start, length} ->
<<before::binary-size(start), match::binary-size(length), rest::binary>> = string
replacement = map[match]
replace(rest, map, pattern, acc <> before <> replacement)
:nomatch ->
acc <> string
end
end
end
my_map = %{"old_value1" => "new_value1", "old_value2" => "new_value2"}
str = "some string .......old_value1 fafdsfd old_value2 faaaaaaa"
IO.puts Main.replace(str, my_map)
輸出:
some string .......new_value1 fafdsfd new_value2 faaaaaaa
是什麼'<<前::二進制大小(開始),匹配::二進制尺寸(長度),其餘::二進制>>'怎麼辦? – Johshi
它將右手邊的值分割成3個大小爲'start','length'和剩下的大小的二進制文件,並將它們分別分配給''before','match'和'rest'。 – Dogbert
但什麼是「<<" and ">>」和「::」? – Johshi