How to string format with unicode args

I’m new to elixir and I want to know how to format a string with unicode args

code

IO.puts(:io_lib.format("hello ~10s ~n", ["👻"]))

result

hello       ð» 

expect

hello       👻

I think you’re sort of over complicating it, you can just interpolate it directly:

iex(2)> ghost = "👻"
"👻"
iex(3)> IO.puts "Hello #{ghost}"
Hello 👻
:ok
3 Likes

Thank you for your answer. It is true that interpolation can solve the problem, but I am in the initial stage of learning elixir. I am not trying to solve a specific problem, I just want to explore more possibilities :wink:

Well, if you want to learn Elixir, use Elixir constructs, not Erlang ones. :stuck_out_tongue: Not yet at least.

According to the docs:

If and only if the Unicode translation modifier is used in the format string (that is, ~ts or ~tc), the resulting list can contain characters beyond the ISO Latin-1 character range (that is, numbers > 255)

I find using the iex h helper very useful in these cases.

iex> h :io_lib.format

Therefore:

iex> IO.puts(:io_lib.format("hello ~10ts ~n", ["👻"]))
hello          👻
4 Likes

thanks! very helpful! :slight_smile: