2015-10-17 14 views
10

Ho una puntura che ha incorporato ":Come si incorporano le virgolette in una stringa di elisir?

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

come posso presentare tale stringa come un valore in Elixir

ad esempio:?

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

Utilizzando ~ s e ~ S non ha aiutato

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)> 

risposta

14

Si può sfuggire le virgolette:

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

C'è una sigil_s per rendere questo più conveniente (c'è anche sigil_S che non interpolare variabili):

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

Le quotazioni sono anche fuggiti quando si utilizza stringhe multilinea (heredocs):

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

Grazie. Quando tiriamo tali stringhe da un file.stream! ("Percorso/a/file") dobbiamo fare lo stesso? su ogni riga restituita? –

+0

L'uso di 'File.Stream' sfuggirà automaticamente alle virgolette. per esempio. 'iex (4)> IO.gets"> "; > "" "" TEST "" ";" \ "\" \ "\" TEST \ "\" \ "\ n" ' – Gazler

+0

Si prega di ~ s e ~ S non ha funzionato vedere gli errori riportati nella domanda modificata sopra –

2

I ~s o ~S sigilli sono la strada da percorrere, è basta uno spazio dopo il segno di uguale:

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\">\"" 
Problemi correlati