But Erlang's treatment of strings as lists of bytes is as elegant as it is impractical. The factor-of-eight storage expansion of text, as well as the copying that occurs during message-passing, cripples Erlang for all but the most performance-insensitive text-processing applications.To understand why Erlang string handling is less efficient than a language like Perl, you need to know that each character uses 8 bytes of memory. That's right -- 8 bytes, not 8 bits! Erlang stores each character as a 32-bit integer, with a 32-bit pointer for the next item in the list (remember, strings are lists of characters.) This was not done out of wanton wastefullness; using such large values means that Erlang can easily handle anything the UNICODE people throw at it, and the decision to represent strings as lists of characters means that a host of built-in Erlang primitives work on strings without any work on our parts. On the down side, this also means strings use a lot of memory, and that access to the nth element takes O(n) time (rather than the O(1) time we would get with strings represented as arrays of characters.) In Erlang, literal strings are enclosed in quotes and may contain linebreaks inline, i.e.
1>A_string = "A literal string with a linebreak (\"\\n\") in it". 2>A_string. "A literal string with a linebreak ("\n")\nin it" 3>io:fwrite(A_string). A literal string with a linebreak ("\n") in it
listslibrary to manipulate the string:
1> lists:sort("Hello"). "Hello" 2> lists:sort("ZYX"). "XYZ" 3> lists:subtract("123212", "212"). "312". 4> lists:suffix(".txt", "test.txt"). true 5> lists:suffix(".txt", "test.html"). false
lists:nth(N, List), where N is a 1-based index in the string:
6> lists:nth(1,A_string) 65
lengthwill tell you how long a string (or any list) is.
7> length(A_string). 46
duplicate. However, this is of limited utility, since you can't modify it anyway:
8> C_String = hd(lists:duplicate(1, A_string)). "A literal string with a linebreak \\n\nin it"
9> F_string = lists:duplicate(5, $*). "*****" 10> G_string = string:chars($*, 5). "*****"
lists:appendto join strings:
11> H_string = lists:append(["Hello, ", "Erlang", "!"]). "Hello, Erlang!"
binarydata type, which is often much more efficient since the data is stored as sequences of bytes. GordonWeakliem and NoelWelsh. Explanations about string handling inefficiencies based on the Erlang FAQ. Many helpful comments and Erlang tips by BengtKleberg. -- BrentAFulgham - 19 Aug 2004