2015-10-17 55 views
10

我已经嵌入刺:你如何在Elixir字符串中嵌入双引号?

tx <iq id="wUcdTMYuYoo41" to="[email protected]" type="set" xmlns="w:profile:picture"> 

我怎么能出现这样的字符串作为药剂的值

例如:

iex> s= "tx <iq id="wUcdTMYuYoo41" to="[email protected]" type="set" xmlns="w:profile:picture">" 

使用382 4和〜S没有帮助

iex(20)> s=~S("tx <iq id="wUcdTMYuYoo41" to="[email protected]" type="set" xmlns="w:profile:picture">")    
** (SyntaxError) iex:20: keyword argument must be followed by space after: w: 

iex(20)> s=~s("tx <iq id="wUcdTMYuYoo41" to="[email protected]" type="set" xmlns="w:profile:picture">") 
** (SyntaxError) iex:20: keyword argument must be followed by space after: w: 

iex(20)> 

回答

14

您可以逃脱双引号:

s ="tx <iq id=\"wUcdTMYuYoo41\" to=\"[email protected]\" type=\"set\" xmlns=\"w:profile:picture\">" 

有一个sigil_s以使其更方便(也有sigil_S不插变量):使用时

s = ~s(tx <iq id="wUcdTMYuYoo41" to="[email protected]" type="set" xmlns="w:profile:picture">) 

行情也逃脱多行字符串(here文档):

""" 
tx <iq id="wUcdTMYuYoo41" to="[email protected]" type="set" xmlns="w:profile:picture"> 
""" 
+0

谢谢。当我们从File.stream!(“path/to/file”)拉出这样的字符串时,我们是否需要做同样的事情?在每一行返回? –

+0

使用'File.Stream'会自动转义引号。例如'iex(4)> IO.gets“>”; >“”“”TEST“”“;”\“\”\“\”TEST \“\”\“\ n”' – Gazler

+0

请〜s和〜S无法正常工作查看编辑问题中报告的错误 –

2

~s~S印记都去了,你的方式刚好在你的等号后需要一个空格:

iex(1)> s = ~s("tx <iq id="wUcdTMYuYoo41" to="[email protected]" type="set" xmlns="w:profile:picture">") 
"\"tx <iq id=\"wUcdTMYuYoo41\" to=\"[email protected]\" type=\"set\" xmlns=\"w:profile:picture\">\"" 

iex(2)> s = ~S("tx <iq id="wUcdTMYuYoo41" to="[email protected]" type="set" xmlns="w:profile:picture">") 
"\"tx <iq id=\"wUcdTMYuYoo41\" to=\"[email protected]\" type=\"set\" xmlns=\"w:profile:picture\">\"" 
相关问题