Make 3 functions into one

Hello Processing foundation
I have this really simple question and I know it might seem very easy to do, but I cannot see how to do it correctly.
I have these 3 functions which i want to compromise to one simple function:

char[] explode(String s) { 
  text[0] = s;
  c = new char[text[0].length()];

  for (int i = 0; i < text[0].length(); i++) {
    c[i] = text[0].charAt(i);
  }

  return c;
}

 char[] explode2(String s2) {
  text[1] = s2;
  c2 = new char[text[1].length()];

  for (int i = 0; i < text[1].length(); i++) {
    c2[i] = text[1].charAt(i);
  }

  return c2;
}

char[] explode3(String s3) { 
  text[2] = s3;
  c3 = new char[text[2].length()];

  for (int i = 0; i < text[2].length(); i++) {
    c3[i] = text[2].charAt(i);
  }

  return c3;
}


void setup() {
  char[] carr1 = explode("Hello"); 
  for (char c : carr1) 
    println(c);
}//

char[] explode(String s3) { 
  return 
    s3.toCharArray();
}

they are almost identical. Difference is text[2] = s3;.

Hence:



String[] textArr=new String[3]; 

void setup() {
  char[] carr1 = explode("Hello", 0); 
  char[] carr2 = explode("You", 1); 
  char[] carr3 = explode("There", 2);

  for (String s : textArr) 
    println(s); 
  for (char c : carr1) 
    println(c);
}//

char[] explode(String s_, int index_) { 
  textArr[index_] = s_;
  return 
    s_.toCharArray();
}

Thank you very much

But what if my string array is 3 dimensional?

What do you mean?

Strings distributed in a cube??

something like:

String[][] ord = {{ "hello", "how", "are", "you" }, 
  {  "hello2", "how2", "are2", "you2" }}; 
1 Like

That would mean that you have several chess boards (2D grids) stacked on top of each other.

on each cell field one word is placed.

String[][][] = new String [3][3][3];

etc.

two-dimensional: a grid

see tutorials Two-Dimensional Arrays / Processing.org

What do you when write here? :slight_smile:

This is a short form for the normal for loop with for (int =0.... )

It says for each String in the array textArr do the following

In every iteration of the loop, s gets the next value in textArr

here is an example with a 2D array of String




String[] textArr=new String[3]; 

String[][] ord =  {
  { "hello", "how", "are", "you" }, 
  {  "hello2", "how2", "are2", "you2" }
}; 


char[][] result = new char [ 2*4 ][];   

void setup() {

  int k=0; 

  for ( String[] arrS : ord ) {
    for ( String s : arrS ) {
      result[k] = explode(s, 0);
      k++;
    }
  }

  // -----------------------

  for (char[] cArr : result)
    for (char c : cArr)
      println(c);
}//

char[] explode(String s_, int index_) { 
  textArr[index_] = s_;
  return 
    s_.toCharArray();
}

Thank you very much Chrisir. This works very well.
But what if I only want the explode()-function to explode specified words in the string-array (ord), and not the whole string-array?
Because first I choose a random word from the string-array, which is put in the variables text[0], text[1], and text[2], and then i want these words to be split up in chars

you can pass the two necessary indexes to explode and explode only that word

e.g. 2,1

Where do I do this in the code?

just in the explode function

in setup you say

      result[k] = explode(2,1);