【问题标题】:How do I check if a element is contained within a list?如何检查一个元素是否包含在列表中?
【发布时间】:2016-06-25 11:29:03
【问题描述】:

如何从列表中获取单词并在 Erlang 中返回 True

catch_word(msg) ->
    Bad = ["BadWord1", "BadWord2"],
    case Bad in msg of
        true ->
            true;
        false ->
            false
    end.

catch_word("Hello, How are u BadWord1").

【问题讨论】:

    标签: erl


    【解决方案1】:

    欢迎来到 Erlang,你可能想试试这个:

    -export([catch_word/2]).
    
    catch_word(Msg,BadWords)->
        catch_word(Msg,BadWords,0).
    
    catch_word(Msg,[],0)->
        false;
    catch_word(Msg,[Word|BadWords],0)->
            catch_word(Msg,BadWords,string:str(Msg,Word));
    catch_word(_,_,N)->
        true.
    

    your_module:catch_word("Hello, How are u BadWord1",["BadWord1", "BadWord2"]).
    

    【讨论】:

      猜你喜欢
      • 2021-06-24
      • 2014-07-31
      • 2012-08-01
      • 2014-04-30
      • 2016-05-12
      • 1970-01-01
      • 2022-11-14
      • 2021-03-02
      相关资源
      最近更新 更多