Results 1 to 4 of 4

Thread: [RESOLVED] Javascript array.join ignoring specified delimiter

  1. #1

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Resolved [RESOLVED] Javascript 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
    Last edited by aconybeare; Nov 12th, 2006 at 04:07 AM. Reason: improve title

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Javascript array.join ignoring specified delimiter

    What browser does it fail in? A command like this:
    ""jani~hege~stale".split("~").join("~")"
    works fine in my Firefox 1.5. It also works in Opera. I was too lazy to test in Konqueror, which didn't like JavaScript being entered directly in the address bar.

    Edit: OK, I know what your error is. Like split(), join() does not modify the object it is called on but instead returns the string it creates. You don't assign this string to anything. Thus what your alert displays is just the default stringification of the array, which uses commas.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: Javascript array.join ignoring specified delimiter

    CornedBee,

    Thanks for your reply, using ie7.

    Haven't tested this, but I take it what you're saying is that I need to assign the join to a variable.

    Code:
    val=val.join("~");
    Cheers Al
    Last edited by aconybeare; Nov 13th, 2006 at 03:31 AM.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: [RESOLVED] Javascript array.join ignoring specified delimiter

    Yes.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width