How to separate the first letter of each word in a string

Hello, I want to separate the first letters of each word in the string:
String animals=(“ant, bat, cat”);

final String ANIMALS = "ant, bat, cat";
final String[] animals = split(ANIMALS, ", ");

for ( int i = 0; i < animals.length; 
  animals[i] = str(animals[i++].charAt(0)) );

println(animals);
exit();

Variable animals[] is an array now. Each of its strings corresponds to 1 index. :grinning:

You can use printArray() to see each 1 in its own line and corresponding index: :sunglasses:

https://Processing.org/reference/printArray_.html

There are many approaches to solve it. But split() was the easiest I’ve come up w/: :money_mouth_face:

BtW, you can make the resultant array to become 1 String again via join(): :wink:

You could do this:

final String ANIMALS = "ant, bat, cat";
final String[] animals = split(ANIMALS, ", ");

for ( int i = 0; i < animals.length; 
  animals[i] = str(animals[i++].charAt(0)) );

String joinedAnimals = join(animals, "");
println(joinedAnimals);
exit();
1 Like

Warning: Your posted code isn’t properly formatted for the forum yet! :grimacing:

The problem is that "stress, level, elimination, exercise, plan, " gotta a trailing ", "! :face_with_thermometer:

You can either remove it or replace split() w/ splitTokens(): :couch_and_lamp:

https://Processing.org/reference/splitTokens_.html

Don’t forget You have the power of Java. Use RegX instead :

“\\b[a-zA-Z]”