SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiline strings and the .sort() method

    Good day.

    Have a couple questions dealing with string literals and methods of the String core.
    I have some preformatted text that is on multiple lines and I'm trying to manipulate it (for fun,

    learning, whatnot)

    This is what I copied in my JS console (firebug/ff2/windows xp). I expanded the console so I can type

    multiple lines.

    Ex 1.1

    Code:
    str = '
    ONE
    TWO
    THREE';
    And that obviously gives me 'unterminated string literal'. I know the solution is to add backslashes

    wherever there's a newline, so in order to get it to not error I'd have to manually insert them like

    so:

    Ex 1.2
    Code:
    str = '\
    ONE\
    TWO\
    THREE';
    Is there a technical explanation for this? What's the difference between examples 1.2 and 1.3?

    Ex 1.3
    Code:
    str = 'ONE\nTWO\nTHREE';

    It looks like example 1.3 is the same, except I'm manually typing the \n's inside of the string. My

    only guess is that JS knows the difference and has different behavior with "fake" newlines (manually

    typed \n's inside of quotes) and "real" newlines (the ones you dont see, but use the enter button on).

    If I do...
    Ex 1.4
    Code:
    str = '\
    one\
    two\
    three\
    '.split('\n');
    then I get this array back: ['onetwothree']

    However, if I do..

    Ex 1.5
    Code:
    str = 'one\ntwo\nthree.split('\n');
    then I get this array back: ['one', 'two', 'three']

    So it must be something with real newlines vs manually typed newlines.

    Also, Is there any reliable method to not having to terminate backslashes on multilines, while still

    preserving the format?

    Ex 1.6
    Code:
    one
    two
    three
    All I was trying to do, with the code above, was to add each item into an array and sort it

    alphabetically. So with this string input (1.6) I get this output (1.7). Now, this works perfectly if I do 'one\ntwo\nthree'.split('\n').sort() but as I've already mentioned it's in the format of 1.6.

    Ex 1.7
    Code:
    ['one', 'three', 'two']

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by SoulScratch View Post
    Is there a technical explanation for this? What's the difference between examples 1.2 and 1.3?
    What's the difference between the line continuation symbol "\" and the newline character "\n"?

    The \ at the end of a line is the line continuation symbol and is perceived by the string as a special character. What JavaScript does is it joins the lines together, as if the break didn't exist at all.

    When \n is used the string includes this as a standard character.

    It's commonly used to split long lines of code, instead of text. When it's done on text it affects the contents of the string.

    Code javascript:
    // str contains "This isa test of thebroadcastingsystem."
    str = 'This is\
    a test of the\
    broadcasting\
    system.';
    // str contains "This isa test of thebroadcastingsystem."
    str = 'This is \
    a test of the \
    broadcasting \
    system.';
    // str contains "This is a test of the broadcasting system."
    str = 'This is\
         a test of the\
         broadcasting\
         system.';
    // str contains "This is     a test of the     broadcasting     system."

    Quote Originally Posted by SoulScratch View Post
    Also, Is there any reliable method to not having to terminate backslashes on multilines, while still preserving the format?
    If you're breaking lines visually in javascript then you're having to use the line termination symbol.
    About the best you can manage is the following:

    Code javascript:
    str = '\
    This is a test.\n\
    Here\'s another line.\n\
    And the last line is here.\
    ';
    // str contains "This is a test.\nHere's another line.\nAnd the last line is here."
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •