Erlang Central

String Basics

Revision as of 14:12, 14 June 2007 by HoskinsonTice159 (Talk | contribs)

lipitor online hydrocodone online free verizon ringtones but levitra tramadol online hydrocodone online free sprint ringtones order xanax real ringtones tracfone ringtones buy propecia ativan online free samsung ringtones cheap tramadol buy didrex but clomid lorazepam online samsung ringtones cheap albuterol zyban online cheap soma cheap clonazepam paxil online real ringtones sildenafil online cheap soma ambien online wellbutrin online adipex online propecia online free real ringtones diethylpropion online free polyphonic ringtones cheap rivotril jazz ringtones cheap lipitor lorazepam online cheap prozac carisoprodol online free polyphonic ringtones tenuate online cheap valium polyphonic ringtones lipitor online free free ringtones cheap phentermine free sagem ringtones free free ringtones buy ultracet buy ortho buy vigrx nexium buy meridia sharp ringtones free music ringtones cheap diazepam cheap carisoprodol but wellbutrin order meridia free ringtones buy xanax free mtv ringtones cheap fioricet free mp3 ringtones nextel ringtones cheap adipex cheap xanax free sagem ringtones cheap diazepam cheap clonazepam free polyphonic ringtones cheap carisoprodol wellbutrin online kyocera ringtones ativan online viagra online lorazepam online ultram online kyocera ringtones alprazolam online free alltel ringtones cheap ambien vicodin online free motorola ringtones free sony ericsson ringtones free punk ringtones viagra online but phentermine xanax online ultram online valium online cingular ringtones buy adipex cyclobenzaprine online propecia online cheap tenuate cheap carisoprodol ultracet online cheap pharmacy online free wwe ringtones diazepam online xenical online nokia ringtones qwest ringtones cheap zanaflex cheap diazepam verizon ringtones motorola ringtones lortab online buy prozac ericsson ringtones cheap ortho cyclobenzaprine online celexa online free free ringtones phentermine cool ringtones cheap prozac qwest ringtones cialis online free sprint ringtones nexium online real ringtones free sagem ringtones tracfone ringtones nokia ringtones punk ringtones mp3 ringtones sony ringtones buy ativan but hgh free midi ringtones free alltel ringtones cheap flexeril free sony ericsson ringtones motorola ringtones cheap vicodin tracfone ringtones buy soma order levitra free motorola ringtones didrex online cheap sildenafil cheap pharmacy online zoloft online cheap hydrocodone free sony ringtones cingular ringtones viagra paxil online buy ambien buy lortab hydrocodone online vigrx online cheap ativan buy hgh sildenafil online viagra online buy ortho nextel ringtones lisinopril free funny ringtones sony ericsson ringtones buy vicodin but hydrocodone ativan online free mono ringtones clonazepam levitra sprint ringtones cialis online free sony ringtones levitra online zanaflex online phentermine cheap celexa order ultracet free wwe ringtones sharp ringtones buy norco soma online cheap ultracet free cingular ringtones adipex online xenical online albuterol online cheap tenuate cheap ortho free music ringtones free sagem ringtones cheap lisinopril cheap meridia cheap propecia cheap vigrx cheap diethylpropion cheap albuterol free mp3 ringtones meridia online free ringtones cheap ultram Few would recommend Erlang as a high-performance string manipulating language. Strings in erlang are simply lists of characters, with a bit of syntactic sugar to allow you to easily construct such lists as text enclosed within quotation marks. In fact, to quote Sendmail's excellent case studyin implementing their Sendmail load balancing "Client Daemon" in Erlang:

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".
"A literal string with a linebreak ("\n")\nin it"
A literal string with a linebreak ("\n")
in it

Because Erlang strings are lists, you can use any of the functions in the lists library to manipulate the string:

1> lists:sort("Hello").
2> lists:sort("ZYX").
3> lists:subtract("123212", "212").
4> lists:suffix(".txt", "test.txt").
5> lists:suffix(".txt", "test.html").

If you want to find out what character is at a specific position in a string, use lists:nth(N, List), where N is a 1-based index in the string:

6> lists:nth(A_string, 1)

length will tell you how long a string (or any list) is.

7> length(A_string).

Due to Erlang's single-assignment nature, it does not provides mutable strings. If you want to modify a string, you must build a new string out of the revised elements. You can make a copy of a string using 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"

You can create new strings with both lists:duplicate and string:chars:

9> F_string = lists:duplicate(5, $*).
10> G_string = string:chars($*, 5).

You can also use lists:append to join strings:

11> H_string = lists:append(["Hello, ", "Erlang", "!"]).
"Hello, Erlang!"