Lifting terms to string

Ulf Wiger etxuwig@REDACTED
Thu Oct 25 22:34:34 CEST 2001


On Thu, 25 Oct 2001, Niels Christensen wrote:

>Hi,
>
>say I have a variable X, the values of which
>I'd like to convert to a string, e.g. if
>X=2 then I'd like to produce "2". Is there a
>way to do that in Erlang?
>
>What I really want is a version of io:fwrite that
>produces strings instead of writing to IoDevices.

The short answer is: io_lib:fwrite/2, which does exactly what
you're asking for.

You can, of course, try to optimize things a bit:

to_string(X) when atom(X) -> atom_to_list(X);
to_string(X) when integer(X) -> integer_to_list(X);
to_string(X) -> lists:flatten(io_lib:fwrite("~w", [X])).

/Uffe
-- 
Ulf Wiger                                    tfn: +46  8 719 81 95
Senior System Architect                      mob: +46 70 519 81 95
Strategic Product & System Management    ATM Multiservice Networks
Data Backbone & Optical Services Division      Ericsson Telecom AB




More information about the erlang-questions mailing list