2015-10-17 54 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\">\"" 
相關問題