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]

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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 131117 1222
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

We're in Beta

About us Mission Statement