Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Confusion in string.split land

Reply
Thread Tools

Confusion in string.split land

 
 
GenghisOne
Guest
Posts: n/a
 
      07-12-2009
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.
 
Reply With Quote
 
 
 
 
David Mark
Guest
Posts: n/a
 
      07-12-2009
On Jul 12, 1:17*am, GenghisOne <mdkach...@gmail.com> 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.
 
Reply With Quote
 
 
 
 
Peter Michaux
Guest
Posts: n/a
 
      07-12-2009
On Jul 11, 10:17*pm, GenghisOne <mdkach...@gmail.com> 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
 
Reply With Quote
 
GenghisOne
Guest
Posts: n/a
 
      07-12-2009
On Jul 11, 10:21*pm, Peter Michaux <petermich...@gmail.com> wrote:
> On Jul 11, 10:17*pm, GenghisOne <mdkach...@gmail.com> 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.
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      07-12-2009
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PIX520 thinks it's under Land Attack 1 Cisco 3 10-07-2005 01:01 PM
MCSD certification - is it still necessary to land a job? Lewis Lang MCSD 26 02-18-2005 07:23 PM
Land Attack News Account Cisco 1 06-17-2004 04:47 PM
MCSD Insights or What I learned on the way to MCSD.NET Cert land.... charlie MCSD 9 02-26-2004 09:17 PM
Novell land Now What MCSE 6 12-11-2003 05:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57