Confusion in string.split land

G

GenghisOne

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.
 
D

David Mark

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.
 
P

Peter Michaux

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>two", "<p>three", ""]

Peter
 
G

GenghisOne

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>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.
 
E

Evertjan.

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);
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top