View Full Version : split string into array?

10-12-2008, 04:46 PM
Hi dudes:)
If I have a pipe delimited string like so "JOHN | FRED | JEFF" how can I split the string into an array and see if FRED exists??

10-12-2008, 04:54 PM
You don't need an array-

var Fred= string.indexOf('FRED')!=-1;

If you already have an array, you can use the Array indexOf method, if it exists,
or roll your own-

var Fred, A= string.split(/\s*\|\|s*/);
while(A.length && !Fred){
Fred= A.shift();

10-12-2008, 04:57 PM
You don't need to split the string to see if 'FRED' exists:
if (Names_in_String.indexOf('FRED') >= 0) { alert('Found: FRED'); }

If you still want to create the array then:
Names_in_Array = new Array();
Names_in_Array = Names_in_String.split('|');

10-12-2008, 05:05 PM
You guys rock! Thanks has been posted.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum