FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   splitting string to array
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: splitting string to array  (Read 3398 times)
Dimitre

WWW
splitting string to array
« on: Dec 10th, 2002, 10:47pm »

Is there a way to "split", or "explode" a string into an Array? or Join one array into a string?
thanks
 
fry


WWW
Re: splitting string to array
« Reply #1 on: Dec 11th, 2002, 12:14am »

splitStrings() does it.
 
Code:
String sentence = "a new feature in p5 that the kids will love";
String words[] = splitStrings(sentence);
 
// or if you have a weird separator character
String another = "if_the_text_has_underscores";
String pieces[] = splitStrings(another, '_');

 
there's also splitInts and splitFloats, which will break up a list of (only) numbers the same way.
 
no join() as yet, but that oughta be added soon. a simple one for the 'pieces' above would be:
 
Code:
String joined = "";
char withWhat = ' ';
for (int i = 0; i < pieces.length; i++) {
  joined += pieces[i] + withWhat;
}

a smarter (faster, more complete), but more complex version (something like what'll go into p5):
 
Code:
// join with no separator
String join(String pieces[]) {
  return join(pieces, 0);
}
 
// join with a separator
String join(String pieces[], char separator) {
  StringBuffer buffer = new StringBuffer();
  for (int i = 0; i < pieces.length; i++) {
    if ((i != 0) && (separator != 0)) buffer.append(separator);
    buffer.append(pieces[i]);
  }
  return buffer.toString();
}
 
Dimitre

WWW
Re: splitting string to array
« Reply #2 on: Dec 11th, 2002, 2:26am »

thanks fry! fine!
 
REAS


WWW
Re: splitting string to array
« Reply #3 on: Dec 11th, 2002, 9:59am »

that's good stuff.
 
Dimitre

WWW
Re: splitting string to array
« Reply #4 on: Dec 11th, 2002, 7:13pm »

now with split I could finish proce55ing 3d viewer for guestpixel tiles.  
http://guestpixel.com

« Last Edit: May 7th, 2003, 5:39pm by Dimitre »  
Pages: 1 

« Previous topic | Next topic »