在erlang语言中将元组转换为字符串 Tuple={,Member}, Tuple_in_string=列表:展平(io_lib:format(“~p”,[Tuple]),

在erlang语言中将元组转换为字符串 Tuple={,Member}, Tuple_in_string=列表:展平(io_lib:format(“~p”,[Tuple]),,erlang,Erlang,其输出为: Tuple={<<"jid">>,Member}, Tuple_in_string=lists:flatten(io_lib:format("~p", [Tuple])), “{,\'sdfs\'}” 但是我希望这个输出没有像这样的斜杠 "{<<\"jid\">>,\"sdfs\"}" “{,成员}” 有什么建议吗 我已经尝试了所有的答案,但最后使用了io:format(“\”~s\“~n”,[Tuple\u in\u str

其输出为:

Tuple={<<"jid">>,Member},

Tuple_in_string=lists:flatten(io_lib:format("~p", [Tuple])),
“{,\'sdfs\'}”
但是我希望这个输出没有像这样的斜杠

"{<<\"jid\">>,\"sdfs\"}"
“{,成员}”
有什么建议吗


我已经尝试了所有的答案,但最后使用了io:format(“\”~s\“~n”,[Tuple\u in\u string])。我要的是“{,Member}”,但它不是字符串。它是一个原子。我需要一个字符串,我可以对其应用concat操作。有指针吗?

您可以这样打印它:

"{<<"jid">>,Member}"
它打印:

io:format("\"~s\"~n", [Tuple_in_string]).
“{,“sdfs”}”

此处的\表示以下“是字符串的一部分,而不是字符串分隔符。它们不存在于字符串本身中。之所以出现,是因为您使用的是精美打印格式~p。如果您使用的是字符串格式~s,它们将不会出现在显示中

"{<<"jid">>,"sdfs"}"

首先,您不需要在此处展平列表:

1> io:format("~p~n",["a \"string\""]).               
"a \"string\""
ok
2> io:format("~s~n",["a \"string\""]).
a "string"
ok
3> length("a \"string\""). % is 10 and not 12                     
10
Erlang的概念是
iodata()
,这意味着可打印的内容可以在嵌套列表中,并且大多数函数都可以处理它们,因此您应该只留下:

Tuple_in_string=lists:flatten(io_lib:format("~p", [Tuple])),
其次,当您使用
~p
时,您告诉Erlang以这样的方式打印术语,以便可以将其复制并粘贴到控制台中。这就是为什么所有双引号都被转义
\“
。使用
~s
,意思是“视为字符串”

1>38>Tuple={,“asdf”}。
{,“asdf”}
2> IODATA=io_lib:format(“~p”,[Tuple])。
[[123,[[60,60,\'jid\',62,62],44,\'asdf\',125]]
3> io:格式(“~s~n”,[IODATA])。
{,“asdf”}
好啊
L=Packet\u在元组形式中={xmlel,,[{,},{,},{xmlel,,[],[{xmlcata,“你好”}]},{xmlel,,[{,},{,},{,},{,\'sds\'},{,“Description”},]},]}。
给我:

L =  Packet_in_tuple_form={xmlel,<<"message">>,[{<<"id">>,<<"rkX6Q-8">>},{<<"to">>,<<"multicast.devlab">>}],[{xmlel,<<"body">>,[],[{xmlcdata,"Hello"}]},{xmlel,<<"addresses">>,[{<<"xmlns">>,<<"http://jabber.org/protocol/address">>}],[{xmlel,<<"address">>,[{<<"type">>,<<"to">>},"{<<\"jid\">>,\"sds\"}",{<<"desc">>,"Description"}],[]}]}]}.
{xmllel,,
[{,},{,}],
[{xmlel,,[],[{xmlcata,“你好”}]},
{xmllel,,
[{,}],
[{xmllel,,
[{,},
“{,\“sds\”}”,
{,“Description”}],
[]}]}]}
地址字段中的\是转义字符。 您可以通过检查字符串的长度来验证这一点。

:io:format(“\”~s\“~n”,[Tuple\u in\u string])。=>“{,”sdfs“}”。当我试图用字符串连接它时,它产生了错误。最后,我在变量V中收集了结果(“{,”sdfs“}”),并且is\u atom(V)是真的。但我需要一个字符串
1> 38> Tuple = {<<"jid">>,"asdf"}.
{<<"jid">>,"asdf"}
2> IODATA = io_lib:format("~p", [Tuple]).
[[123,[[60,60,"\"jid\"",62,62],44,"\"asdf\""],125]]
3> io:format("~s~n", [IODATA]).
{<<"jid">>,"asdf"}
ok
L =  Packet_in_tuple_form={xmlel,<<"message">>,[{<<"id">>,<<"rkX6Q-8">>},{<<"to">>,<<"multicast.devlab">>}],[{xmlel,<<"body">>,[],[{xmlcdata,"Hello"}]},{xmlel,<<"addresses">>,[{<<"xmlns">>,<<"http://jabber.org/protocol/address">>}],[{xmlel,<<"address">>,[{<<"type">>,<<"to">>},"{<<\"jid\">>,\"sds\"}",{<<"desc">>,"Description"}],[]}]}]}.
{xmlel,<<"message">>,
   [{<<"id">>,<<"rkX6Q-8">>},{<<"to">>,<<"multicast.devlab">>}],
   [{xmlel,<<"body">>,[],[{xmlcdata,"Hello"}]},
    {xmlel,<<"addresses">>,
           [{<<"xmlns">>,<<"http://jabber.org/protocol/address">>}],
           [{xmlel,<<"address">>,
                   [{<<"type">>,<<"to">>},
                    "{<<\"jid\">>,\"sds\"}",
                    {<<"desc">>,"Description"}],
                   []}]}]}