We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › converting a formatted string to variables...
Page Index Toggle Pages: 1
converting a formatted string to variables... (Read 588 times)
converting a formatted string to variables...
Oct 10th, 2005, 12:06am
 
Hi,
I'm wondering if there's an easy way to take a string like "ID:4 Direction:1 Text:'A short phrase in single quotes'" to
int id=4;
int direction[id]=1;
String txt[id]="A short phrase in single quotes";
in Processing. In C string.h has a simple way of doing this with the string "ID:%d Direction:%d Text:'%s'". Is there any equivalent?
Thanks,
Joseph
Re: converting a formatted string to variables...
Reply #1 - Oct 10th, 2005, 12:59pm
 
Unless you feel like writing us a parsing library, short answer, 'no'.
However, you could get around the operation in a very simple fashion with some lateral thinking. Try imagining your parameter String like this:
Code:

String foo = "ID:4:Direction:4:text:hello"

Which can be split into an array with:
Code:

String [] params = split(foo,":");

Now every second article in the array is a parameter. If you wanted to keep the format of the original String then you can perform a two-fold split. Split for spaces then for colons. If these parameters are going to be entered by users I would suggest a bit of overkill with the command trim() and with the String method equalsIgnoreCase().
Re: converting a formatted string to variables...
Reply #2 - Oct 10th, 2005, 4:58pm
 
you can also include more characters in the split() function:

by adding a space:
String [] params = split(foo,": ");
you won't need to change your output format.

and toInt() will convert individual params[] to integers as needed.
Page Index Toggle Pages: 1