...

View Full Version : split string into array?



nikko50
10-12-2008, 05: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??
Tracy:)

mrhoo
10-12-2008, 05: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();
}

jmrker
10-12-2008, 05: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('|');

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum