[erlang-questions] Which is best? string:concat or ++?
Mon May 7 16:17:33 CEST 2012
Given you generate chunks of HTML and that this will undoubtedly be
pushed over a socket at some point, what you want to use is IO Lists.
They are lists of either bytes (integers from 0 to 255), binaries, or
other IO lists. This means that functions that accept IO lists can
accept items such as [$H, $e, [$l, <<"lo">>, " "], [[["W","o"],
<<"rl">>]] | [<<"d">>]]. When this happens, the Erlang VM will just
flatten the list as it needs to do it to obtain the sequence of
characters Hello World.
Any function from the io module, file module, TCP and UDP sockets will
be able to handle them. Some library functions, such as some coming from
the unicode module and all of the functions from the re (for regular
expressions) module will also handle them, to name a few.
So in your case, to join string A and B, no matter whether they're
binaries or lists, just do [A,B] and that's your new string. You'll save
a lot in terms of rewriting terms and whatnot doing things that way.
On 12-05-07 9:31 AM, Paul Barry wrote:
> Hi folks.
> This might be a case of a dumb question, but here goes anyway. :-)
> I have two strings, A and B. Is it better to use:
> string:concat(A, B)
> A ++ B
> when combining them to create a new string? I'm writing some code
> that generates a chunk of HTML. I know that using ++ on big lists is
> regarded as a "no-no", but is it acceptable here?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions