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.
You can use printArray() to see each 1 in its own line and corresponding index:
There are many approaches to solve it. But split() was the easiest I’ve come up w/:
BtW, you can make the resultant array to become 1 String again via join():
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!
The problem is that "stress, level, elimination, exercise, plan, "
gotta a trailing ", "
!
You can either remove it or replace split() w/ splitTokens():
Don’t forget You have the power of Java. Use RegX instead :
“\\b[a-zA-Z]”