zcking

zcking

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?

Showing Posts 1 to 6

kip

kip

ex_cldr Core Team

I think IO.inspect may be what you want:

iex> s = "raw \n string \t here"  
iex> IO.puts s
raw 
 string          here
:ok
iex> IO.inspect s
"raw \n string \t here"

Note that these are not “interpreted”; in the string is the actual character. It is only IO.inspect that is interpreting the string and replacing the actual character with the escape sequence \n and \t.

zcking

zcking OP

Apologies, I made a mistake by saying my goal is to “print” the string. I’m writing a function which returns a string, that way the client of my library can print the string wherever they want, or send it to some other I/O mechanism. IO.inspect/1 prints to stdout and returns the string as-is, without escaping. I suppose alternatively, I could add a device argument which defaults to :stdio in favor of the stdlib’s IO.inspect function, and then instead of returning a string I could have my function call IO.inspect/2 and pass the device argument to it.

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.

zcking

zcking OP

Yes!! Kernel.inspect/2 is exactly what I was looking for, thank you! As an elixir rookie, I didn’t know about the Kernel.inspect function–only the IO.inspect, and somehow missed it in the docs.

9mm

9mm

Can I hijack this thread and ask how you’re supposed to print stuff without them being interpreted as char lists in the output?

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]
— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews