0

I am new to processing and trying to figure out a way to create an array of all the characters within a string. Currently I Have:

String[] words = {"hello", "devak", "road", "duck", "face"};
String theWord = words[int(random(0,words.length))];

I've been googling and haven't found a good solution yet. Thanks in advance.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
Muhammad Shahab
  • 119
  • 1
  • 1
  • 4

1 Answers1

0

In addition to the comment you posted (which perhaps should have been an answer), there are a ton of ways to split a String.

The most obvious solution might be the String.split() function. If you give that function an empty String "" as an argument, it will split every character:

void setup() { 
  String myString = "testing testing 123";
  String[] chars = myString.split("");
  for (String c : chars) {
    println(c);
  }
}

You could also just use the String.charAt() function:

void setup() { 
  String myString = "testing testing 123";
  for (int i = 0; i < myString.length(); i++) {
    char c = myString.charAt(i);
    println(c);
  }
}
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • I did that second one and it worked, but now whenever i try to check a character with an if statement, I get an error about how characters aren't strings. :/ – Muhammad Shahab Dec 01 '15 at 16:44
  • Well, characters aren't Strings. That's true. Try posting another question with an updated [MCVE](http://stackoverflow.com/help/mcve)- you don't have to include the splitting part, just hardcode the character and what you're comparing it to. – Kevin Workman Dec 01 '15 at 16:53