zcking

zcking

Printing raw string stored in variable

I’m trying to figure out how to print out the contents of a string variable, without it interpreting the characters like \n and \t as newlines and tabs. I want to display the “raw” string such as you would do with ~S(raw \n string \t here), however, I cannot pass a variable or interpolate with that sigil.

Is there an idiomatic way to do this with Elixir?

Marked As Solved

kip

kip

ex_cldr Core Team

Or just call Kernel.inspect/2:

iex> inspect(s)
"\"raw \\n string \\t here\""
iex> inspect(s) |>  String.trim("\"") 
"raw \\n string \\t here"

Yes, the leading and trailing part might need to be trimmed if you need. But it seems closer to what you’re after - an escaped string.

Also Liked

kip

kip

ex_cldr Core Team

Kernel.inspect has options to control this via Inspect.Opts:

• :charlists - when :as_charlists all lists will be printed as char
lists, non-printable elements will be escaped.
When :as_lists all lists will be printed as lists.

When the default :infer, the list will be printed as a charlist if it is
printable, otherwise as list.

As an example:

iex(6)> inspect [32,33,34], charlists: :as_lists
"[32, 33, 34]"
iex(7)> inspect [32,33,34]                      
"' !\"'"

You can tell IEx to default to this behaviour by:

hex> IEx.configure [inspect: [charlists: :as_lists]]
:ok
iex> [32,33,34]                                     
[32, 33, 34]

Last Post!

kip

kip

ex_cldr Core Team

Kernel.inspect has options to control this via Inspect.Opts:

• :charlists - when :as_charlists all lists will be printed as char
lists, non-printable elements will be escaped.
When :as_lists all lists will be printed as lists.

When the default :infer, the list will be printed as a charlist if it is
printable, otherwise as list.

As an example:

iex(6)> inspect [32,33,34], charlists: :as_lists
"[32, 33, 34]"
iex(7)> inspect [32,33,34]                      
"' !\"'"

You can tell IEx to default to this behaviour by:

hex> IEx.configure [inspect: [charlists: :as_lists]]
:ok
iex> [32,33,34]                                     
[32, 33, 34]

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement