Welcome to WebmasterWorld Guest from 54.145.222.231

Forum Moderators: open

Message Too Old, No Replies

stripping a character away from a string

JS

   
10:48 pm on Feb 15, 2002 (gmt 0)

10+ Year Member



Hi all,

What would be the best way for me to go about strpping the last character in an array?
For example:
var Dimensional = [["0","0"],["1","1"],["2","2"],["3","3"],];

Whats the best way for me to get rid of that last commma?

[add]In JavaScript that is.[/add]

Thanks

11:06 pm on Feb 15, 2002 (gmt 0)

10+ Year Member



I'm not sure if i understand the question correctly, but if you want to replace that last comma if you have a String like this;

var Dimensional = '[["0","0"],["1","1"],["2","2"],["3","3"],]';

Then you could get rit of the last comma by using a regular expression

re = /\]\,\]/gi;
var newstr=str.replace(re, ']]');

6:11 am on Feb 20, 2002 (gmt 0)

10+ Year Member



quick question.

How does it exactly know to take off from the Dimensional Array?

6:18 am on Feb 20, 2002 (gmt 0)

10+ Year Member



I'm very sorry, but i don't understand what you're saying. Please rephrase the question.
6:19 am on Feb 20, 2002 (gmt 0)

WebmasterWorld Senior Member txbakers is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Hey there. In VB script it's as simple as this:
dim harry as integer
harry = len (proplist)

proplist = left(proplist, harry - 1)

but in Javascript it's a bitmore complicated. I would do it in VB script......

6:33 am on Feb 20, 2002 (gmt 0)

10+ Year Member



Hi,

My question is about the code you posted.

The var newstr = str.replace(re, ']]');

I know I'm supposed to replace the str part with the variable name I'm trying to work with. But everytime I replace it to Dimensional.replace(re, ']]');
It ends up giving me an error.
The error is :

Object doesn't support this property or method.

So I was just wondering if you can explain how to use the code a little more so that I can see if I'm doing something wrong.

Oh, thanks txbakers, but I've been told to do it strictly in JS.

:)

9:35 pm on Feb 20, 2002 (gmt 0)

10+ Year Member



I'll butt in:

var Dimensional = '[["0","0"],["1","1"],["2","2"],["3","3"],]';

re = /\]\,\]/gi;

var Dimensional=Dimensional.replace(re, ']]');