Confusion in string.split land

Discussion in 'Javascript' started by GenghisOne, Jul 12, 2009.

  1. GenghisOne

    GenghisOne Guest

    I am totally confused by this string.split thing..

    When I try this...
    var myString = "one, two, three";
    myArray = myString.split(",");
    document.write(myArray.length);

    ....I get back 3 items. This makes sense to me.


    But when I try this...
    var myString = "<p>one</p><p>two</p><p>three</p>";
    myArray = myString.split("</p>");
    document.write(myArray.length);

    ....I get back 4 items. What gives? Shouldn't myArray.length return 3
    as well.


    Any help would be most sincerely appreciated and my apologies in
    advance for such a basic question.
     
    GenghisOne, Jul 12, 2009
    #1
    1. Advertising

  2. GenghisOne

    David Mark Guest

    On Jul 12, 1:17 am, GenghisOne <> wrote:
    > I am totally confused by this string.split thing..
    >
    > When I try this...
    > var myString = "one, two, three";
    > myArray = myString.split(",");
    > document.write(myArray.length);
    >
    > ...I get back 3 items. This makes sense to me.
    >
    > But when I try this...
    > var myString = "<p>one</p><p>two</p><p>three</p>";
    > myArray = myString.split("</p>");
    > document.write(myArray.length);
    >
    > ...I get back 4 items. What gives? Shouldn't myArray.length return 3
    > as well.


    No.

    >
    > Any help would be most sincerely appreciated and my apologies in
    > advance for such a basic question.


    Count the occurrences of the delimiter in each case.
     
    David Mark, Jul 12, 2009
    #2
    1. Advertising

  3. On Jul 11, 10:17 pm, GenghisOne <> wrote:
    > I am totally confused by this string.split thing..
    >
    > When I try this...
    > var myString = "one, two, three";
    > myArray = myString.split(",");
    > document.write(myArray.length);
    >
    > ...I get back 3 items. This makes sense to me.
    >
    > But when I try this...
    > var myString = "<p>one</p><p>two</p><p>three</p>";
    > myArray = myString.split("</p>");
    > document.write(myArray.length);
    >
    > ...I get back 4 items. What gives? Shouldn't myArray.length return 3
    > as well.
    >
    > Any help would be most sincerely appreciated and my apologies in
    > advance for such a basic question.


    Your HTML string ends in </p>. In string.split land, there is an empty
    string after that third occurrence of </p> and so there are four
    elements in the result.

    Firebug console session...

    >>> "<p>one</p><p>two</p><p>three</p>".split("</p>");

    ["<p>one", "<p>two", "<p>three", ""]

    Peter
     
    Peter Michaux, Jul 12, 2009
    #3
  4. GenghisOne

    GenghisOne Guest

    On Jul 11, 10:21 pm, Peter Michaux <> wrote:
    > On Jul 11, 10:17 pm, GenghisOne <> wrote:
    >
    >
    >
    > > I am totally confused by this string.split thing..

    >
    > > When I try this...
    > > var myString = "one, two, three";
    > > myArray = myString.split(",");
    > > document.write(myArray.length);

    >
    > > ...I get back 3 items. This makes sense to me.

    >
    > > But when I try this...
    > > var myString = "<p>one</p><p>two</p><p>three</p>";
    > > myArray = myString.split("</p>");
    > > document.write(myArray.length);

    >
    > > ...I get back 4 items. What gives? Shouldn't myArray.length return 3
    > > as well.

    >
    > > Any help would be most sincerely appreciated and my apologies in
    > > advance for such a basic question.

    >
    > Your HTML string ends in </p>. In string.split land, there is an empty
    > string after that third occurrence of </p> and so there are four
    > elements in the result.
    >
    > Firebug console session...
    >
    > >>> "<p>one</p><p>two</p><p>three</p>".split("</p>");

    >
    > ["<p>one", "<p>two", "<p>three", ""]
    >
    > Peter


    Hi Peter

    Thanks for the insights...

    After looking at your Firebug console results, I started to ask
    myself...where did my </p> tags go? And then it dawned on me...the
    split function deletes the split term you use.

    So maybe what I'm really trying to ask is this...

    What's the best way to convert the following string into an array?

    var myString = "<p>one</p><p>two</p><p>three</p>";

    In my mind, the perfect conversion function would bring back an array
    of three items.

    Thx much.
     
    GenghisOne, Jul 12, 2009
    #4
  5. GenghisOne

    Evertjan. Guest

    GenghisOne wrote on 12 jul 2009 in comp.lang.javascript:

    > What's the best way to convert the following string into an array?


    Stop asking for the "best" way, that is subject to the whims of the
    programmer uder influence of "fasted", "shortest", "easiest", "most
    appealing", "best to remember", etc.

    >
    > var myString = "<p>one</p><p>two</p><p>three</p>";
    >
    > In my mind, the perfect conversion function would bring back an array
    > of three items.


    Your mind is wrong, as you do not specify the resulting array content.

    Let us suppose you want the text content.

    var myString = "<p>one</p><p>two</p><p>three</p>";
    myString = myString.replace(/(^<p>)|(<\/p>)/g,'');
    var myArray = myString.split('<p>');
    alert(myArray.length);

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Jul 12, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug Holland

    Adventures in Whidbey land...

    Doug Holland, Jan 6, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    354
    Yan-Hong Huang[MSFT]
    Jan 6, 2004
  2. Kevin
    Replies:
    4
    Views:
    429
    Irrwahn Grausewitz
    Oct 17, 2003
  3. Carlos Ribeiro
    Replies:
    11
    Views:
    702
    Alex Martelli
    Sep 17, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    246
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    618
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page