Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts

    split string into array?

    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

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    You don't need an array-

    Code:
    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-

    Code:
    var Fred, A= string.split(/\s*\|\|s*/);
    while(A.length && !Fred){
    	Fred= A.shift();
    }
    Last edited by mrhoo; 10-12-2008 at 10:04 PM.

  • Users who have thanked mrhoo for this post:

    nikko50 (10-12-2008)

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,073
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

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

  • Users who have thanked jmrker for this post:

    nikko50 (10-12-2008)

  • #4
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts
    You guys rock! Thanks has been posted.
    Tracy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •