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 3 of 3
  1. #1
    Regular Coder Running Bear's Avatar
    Join Date
    Aug 2002
    Location
    UK
    Posts
    104
    Thanks
    1
    Thanked 0 Times in 0 Posts

    array.join ignoring specified delimiter

    Hi,
    Can anyone see why the below join isn't working? It insists on joining the string with a comma ",". It's not the "~" as I've tried changing the delimiter to dot "." and "|" with no effect and it's not the splice as I've commented it out and that didn't make any difference either


    Code:
    <html>
    <body>
    
    <script type="text/javascript">
    var item=1;
    var val="jani~hege~stale";
    val=val.split("~");
    if(val.length>=item){
      val.splice(item,1);
      val.join("~");
    }
    alert(val);
    
    </script>
    
    </body>
    </html>
    Any help will be greatly appreciated

    Cheers Al

  • #2
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    The join function isn't quite the same as the splice function.
    join doesn't actually alter the array itself, but returns a string version of it.

    You can either commit this string to a variable beforehand and alert that variable or alert the returned string directly in the alert function.

    Code:
    var item = 1;
    var val = 'jani~hege~stale';
    val = val.split('~'); // defining val as an array
    if (val.length >= item) {
    	val.splice(item,1);
    	val = val.join('~'); // redefining val as a string
    }
    
    alert(val);
    The difference can be more clearly demonstrated with this slightly altered version of your script.

    Code:
    var item = 1;
    var val = "jani~hege~stale";
    val = val.split("~");
    if (val.length >= item) {
    	val.splice(item,1);
    	nuval = val.join("~");
    }
    
    alert('val='+val+'\nnuval='+nuval);
    Last edited by Bill Posters; 11-12-2006 at 11:38 AM.

  • #3
    Regular Coder Running Bear's Avatar
    Join Date
    Aug 2002
    Location
    UK
    Posts
    104
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Bill,

    Thanks very much, most appreciated!

    Cheers Al
    Last edited by Running Bear; 11-12-2006 at 01:01 PM.


  •  

    Posting Permissions

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