如何在Elixir字符串中插入双引号? [英] How do you embed double-quotes an Elixir string?

查看:84
本文介绍了如何在Elixir字符串中插入双引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌入了 的字符串:

I have a sting that has embedded ":

tx <iq id="wUcdTMYuYoo41" to="2348138248411@" type="set" xmlns="w:profile:picture">

如何在Elixir中将这样的字符串表示为值?

how can i present such a string as a value in Elixir?

例如:

iex> s= "tx <iq id="wUcdTMYuYoo41" to="2348138248411@" type="set" xmlns="w:profile:picture">"

使用〜s和〜S没有帮助

Using ~s and ~S did not help

iex(20)> s=~S("tx <iq id="wUcdTMYuYoo41" to="2348138248411@" 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="2348138248411@" type="set" xmlns="w:profile:picture">")
** (SyntaxError) iex:20: keyword argument must be followed by space after: w:

iex(20)> 


推荐答案

您可以转义双引号:

s ="tx <iq id=\"wUcdTMYuYoo41\" to=\"2348138248411@\" type=\"set\" xmlns=\"w:profile:picture\">"

有一个 sigil_s 为了使此操作更加方便(还存在 sigil_S 't插入变量):

There is a sigil_s to make this more convenient (there is also sigil_S which doesn't interpolate variables):

s = ~s(tx <iq id="wUcdTMYuYoo41" to="2348138248411@" type="set" xmlns="w:profile:picture">)

行情也被转义使用多行字符串(heredocs)时:

Quotes are also escaped when using multi-line strings (heredocs):

"""
tx <iq id="wUcdTMYuYoo41" to="2348138248411@" type="set" xmlns="w:profile:picture">
"""

这篇关于如何在Elixir字符串中插入双引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆