【问题标题】:Converting string to tuple in Erlang在 Erlang 中将字符串转换为元组
【发布时间】:2015-04-19 03:37:46
【问题描述】:

如何在 Erlang 中将字符串转换为元组?

A = "{"hi","how"}"

我希望它转换成

B = {"hi","how"}.

当我调用函数 list_to_tuple(A) 时,它会给出输出:

{123,60,60,34,106,105,100,34,62,62,44,34,104,105,34,125}

而不是{"hi","how"}

【问题讨论】:

    标签: string erlang tuples ejabberd


    【解决方案1】:

    您应该使用erl_scan 模块对字符串进行标记,并使用erl_parse 将标记转换为erlang 术语。

    % Note the '.' at the end of the expression inside string.
    % The string has to be a valid expression terminated by a '.'.
    1> Str = "{\"x\",\"y\"}.".  
    "{\"x\",\"y\"}."
    2> {ok, Ts, _} = erl_scan:string(Str).
    {ok,[{'{',1},
         {string,1,"x"},
         {',',1},
         {string,1,"y"},
         {'}',1},
         {dot,1}],
        1}
    3> {ok, Tup} = erl_parse:parse_term(Ts).
    {ok,{"x","y"}}
    4> Tup.
    {"x","y"}
    

    【讨论】:

      猜你喜欢
      • 2015-04-20
      • 1970-01-01
      • 1970-01-01
      • 2017-04-30
      • 2012-01-19
      • 1970-01-01
      相关资源
      最近更新 更多