Click to See Complete Forum and Search --> : Splitting a variable


damon2003
10-31-2003, 06:04 AM
I have a variable that contains this.

worddescriptionhere numberhere
e.g. sdsdgdsgsgd 100


I need to know a method os splitting the first part which is text from the second half of the variable which is a number, and puting them in their own seaparate variables. I can place as many spaces between the text and number as necessary or place symbols there,
any ideas?

Charles
10-31-2003, 06:17 AM
<script type="text/javascript">
<!--
function Foo () {for (var j=0; j<arguments.length; j++) {this.add(arguments[j])}};

Foo.prototype.add = function (s) {if (/(\w+)\s+(\d+)/.test(s)) this[RegExp.$1] = RegExp.$2};

foo = new Foo ('sdsdgdsgsgd 100', 'weeeeeeee 200');
alert(foo.weeeeeeee);

// or

fie = new Foo ();
fie.add('sdsdgdsgsgd 100');
fie.add('weeeeeeee 200');
alert(fie.sdsdgdsgsgd);
// -->
</script>

damon2003
10-31-2003, 06:28 AM
Hi
the variable I have is like this
var newValue;
does that function work is that vaiable is put through that function?

Charles
10-31-2003, 06:34 AM
<script type="text/javascript">
<!--
function Foo () {for (var j=0; j<arguments.length; j++) {this.add(arguments[j])}};

Foo.prototype.add = function (s) {if (/(\w+)\s+(\d+)/.test(s)) this[RegExp.$1] = RegExp.$2};

newValue = 'sdsdgdsgsgd 100';

foo = new Foo (newValue);
alert(foo.sdsdgdsgsgd);

// or

fie = new Foo ();
fie.add('sdsdgdsgsgd 100');
alert(fie.sdsdgdsgsgd);
// -->
</script>

damon2003
10-31-2003, 06:39 AM
Hi
thanks,
but what I mean is that isnt this assuming youy know what the first part of the variable is.
Actually we only know that it is a text description, and we need something to give us this in its own variable

Charles
10-31-2003, 06:42 AM
I think that you ned to describe your project a little more clearly. What exactly are you trying to accomplish? And note, if you cannot explain it to me, a fellow human, you never will be able to explain it to a computer.

damon2003
10-31-2003, 06:49 AM
Right I have a variable that contains a text description. This description could be one or more words. Then after the description I have a number, i.e. 1111. I can I need to separate these into two seaparate variables. I know I can control the number of spaces bewteen the text and the number before it gets stored and I think this would help the separation process. so if the original variable was:
aaaaa 111
I want var a = aaaaa
var b = 111
have to assume you do not know any of the original value, only that they are separated by a number of spaces that can be defined

Charles
10-31-2003, 07:03 AM
<script type="text/javascript">
<!--
function Pair (s) {if (/(\w+)\s+(\d+)/.test(s)) {this.key = RegExp.$1; this.value = RegExp.$2}};

newValue = new Pair ('sdsdgdsgsgd 100');
alert(newValue.key);
alert(newValue.value);
// -->
</script>