将List转换为括号中的字符串单引号元素 - Erlang

时间:2017-03-20 18:52:29

标签: erlang

我有一个清单

["user2","user","user4"]

我想转换成这样的字符串:

  2> "('user2','user','user4')".
"('user2','user','user4')"

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

这是使用列表推导将单引号添加到开头和结尾的一种方式,然后string:join/2将它们与逗号分隔符连接起来:

1> ["'" ++ X ++ "'" || X <- ["user2","user","user4"]].
["'user2'","'user'","'user4'"]
2> string:join(["'" ++ X ++ "'" || X <- ["user2","user","user4"]], ",").
"'user2','user','user4'"
3> "(" ++ string:join(["'" ++ X ++ "'" || X <- ["user2","user","user4"]], ",") ++ ")".
"('user2','user','user4')"