...

View Full Version : array.join ignoring specified delimiter



Running Bear
11-12-2006, 09:06 AM
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



<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

Bill Posters
11-12-2006, 10:35 AM
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.


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.


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

Running Bear
11-12-2006, 11:37 AM
Bill,

Thanks very much, most appreciated!

Cheers Al



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum