I want to check if a string contains a specific character ("*") and if so, I want to replace it with another character ("%").
Like this: String = "John*"
Changes to... String = "John%"
Thank you
Join Stack Overflow to learn, share knowledge, and build your career.
Straightforward:
-module(replace).
-export([replace/3]).
replace([], _, _) -> [];
replace([H|T], P, R) ->
[ if H =:= P -> R;
true -> H
end | replace(T, P, R)].
Usage:
$ erl
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.3 (abort with ^G)
1> c(replace).
{ok,replace}
2> replace:replace("John*", $*, $%).
"John%"
binary
module uses binaries for its function args, so those need to be binaries (e.g.,<<"John*">>
). – Derek Brown Apr 23 '18 at 15:37string:replace("John*", "*", "%").
? – Dogbert Apr 24 '18 at 4:05