Thursday, January 21, 2010

Multi-line strings

Javascript doesn't have multi-line strings, but offers a variety of hacks ranging from concatination ("line 1\n"
+"def")
to the CDATA hack, but as always, your milage will vary depending upon your browser.

Python has dedicated multi-line string denominators, namely the triple-quote -- """ and '''. Quite workable, but not as elegant as simply allowing newlines in a string.

C and shell have the backslash:

char * s = "line 1\n\
line 2";

Haskell extends that to the double-backslash:

s = "line 1\n\
\line 2"


PHP, in a rare moment of elegance and usability, treats a newline in a string as a newline.

$s = "line 1
line 2";


Now why is that so hard?

Update: Jake is right: C-style backslashes work in Javascript (at least on Firefox).

1 comment:

Jake said...

from your link it looks like you can do the C backslash in Javascript as well like this:

s = "this is a \
multiline string"

is that true? even if it is, I don't like it because it makes your indentation ugly