Replace 如何在Erlang中替换字符串中的字符?

Replace 如何在Erlang中替换字符串中的字符?,replace,erlang,character,Replace,Erlang,Character,我想检查字符串是否包含特定字符(“*”),如果是,我想用另一个字符(“%”)替换它 像这样: String=“John*” 更改为。。。 String=“John%” 谢谢简单明了: -module(replace). -export([replace/3]). replace([], _, _) -> []; replace([H|T], P, R) -> [ if H =:= P -> R; true -> H end | r

我想检查字符串是否包含特定字符(“*”),如果是,我想用另一个字符(“%”)替换它

像这样: String=“John*”

更改为。。。 String=“John%”

谢谢

简单明了:

-module(replace).

-export([replace/3]).

replace([], _, _) -> [];
replace([H|T], P, R) ->
    [ if H =:= P -> R;
         true -> H
      end | replace(T, P, R)].
用法:

$ 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%"
直截了当:

-module(replace).

-export([replace/3]).

replace([], _, _) -> [];
replace([H|T], P, R) ->
    [ if H =:= P -> R;
         true -> H
      end | replace(T, P, R)].
用法:

$ 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%"
io:format(字符串:替换(“John*”、“*”、“%”)。 您可以使用。

>io:format(字符串:replace(“John*”、“*”、“%”)。

您可以使用。

Elang还是Erlang?如果您要求erlang:result=binary:replace(“John*”、“*”、“%”、[global])。
binary
模块使用二进制文件作为其函数参数,因此这些必须是二进制文件(例如,
)。erlang,我指的是erlang。谢谢你们两位。
string:replace(“John*”、“*”、“%”)。
?Elang还是Erlang?如果您要求erlang:result=binary:replace(“John*”、“*”、“%”、[global])。
binary
模块使用二进制文件作为其函数参数,因此这些必须是二进制文件(例如,
)。erlang,我指的是erlang。谢谢你们两位。
string:replace(“John*”、“*”、“%”)。谢谢。谢谢,谢谢。我很感激。