Is there any way to split a string into an array of characters?

Is there any way that I can split up a string into an array of its individual components? Like, for example, if I had the string “hello?”, is there any way I could convert that into an array in the form {“h”,“e”,“l”,“l”,“o”,"?"} ? I’m aware that there’s a function called split, but it only appears to work if there’s something separating each part. (so if a==“h-e-ll-o”, split(a,"-") will give you an array of the form {“h”,“e”,“ll”,“o”}). I’ve tried leaving the second parameter blank, but that wouldn’t compile, and I tried setting the string in that field to “”, and processing decided to have an angry nap. I’ve also tried looking into the splitTokens function, but it doesn’t really seem to be much different.

Is there any way I can split a string into its base components? Or is this just a lost cause?

1 Like

Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#toCharArray()

1 Like

thank you so much! Side note, why isn’t substring listed in the processing reference page?

Processing.org/reference/String_substring_.html

oh…well why can’t I find it here? https://processing.org/reference/

It’s inside String’s reference: Processing.org/reference/String.html

2 Likes

Note that this is true for String, and is also true for PImage, PGraphics, PFont, PShader, PVector, and everything under listed under composite on the reference – such as FloatList, HashMap, IntDict, JSONArray, Table, and more. All the classes – which are capitalized – have their methods listed under them, not on the main reference page.

2 Likes

Yeah, well, I remember, I found this confusing when I began, because there is even a String section on the reference page, but the really interesting part is not there, but on the String sub-page…

Also, PVector is not in the section data type, but math…

:wink:

1 Like

You had me at "Hello?"

https://processing.org/reference/String_charAt_.html

You can adapt this to store characters in an array:

  String s = "Hello?";
  for(int i = 0; i < s.length(); i++)
    {
    char c = s.charAt(i);
    println(c);


:)

1 Like