Converting a text string to an array

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

Converting a text string to an array

Postby Morley » Tue Dec 28, 2004 3:05 am

An earlier routine has assembled a text string "string" with an unknown number of elements. I want to turn it into an array and then pop the final element. Therefore I use this code:
Code: Select all
var string = "white,blue,red,green";
var myArray = new Array(string);
var lastelement = myArray.pop();
application.output(lastelement); // produces "white,blue,red,green" when I expected just "green".
What's going on here? I'm misunderstanding the required syntax.
Morley Chalmers
7Office Inc.
User avatar
Morley
 
Posts: 891
Joined: Fri Apr 25, 2003 4:54 pm
Location: Toronto, Canada

Postby jcompagner » Tue Dec 28, 2004 11:33 am

why do you expect that?
You are inserting a string that has comma's yes but why should array do something with that?

It is just one string that gets one entry.

if you want to convert that string to an array look at the string.split() method
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8841
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet

Postby Morley » Tue Dec 28, 2004 6:55 pm

Thanks. Split() is exactly what I need in this situation. Thanks again.
Morley Chalmers
7Office Inc.
User avatar
Morley
 
Posts: 891
Joined: Fri Apr 25, 2003 4:54 pm
Location: Toronto, Canada


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 4 guests