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

**URL:** <https://discourse.processing.org/t/how-to-separate-the-first-letter-of-each-word-in-a-string/1449>\
**Category:** Coding Questions\
**Created:** [July 3, 2018, 12:07am UTC](https://discourse.processing.org/t/how-to-separate-the-first-letter-of-each-word-in-a-string/1449 "2018-07-03T00:07:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nerdboybi](https://avatars.discourse-cdn.com/v4/letter/n/a88e4f/32.png) [@nerdboybi](https://discourse.processing.org/u/nerdboybi)\
**Post date:** [July 3, 2018, 12:07am UTC](https://discourse.processing.org/t/how-to-separate-the-first-letter-of-each-word-in-a-string/1449/1 "2018-07-03T00:07:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 3, 2018, 1:16am UTC](https://discourse.processing.org/t/how-to-separate-the-first-letter-of-each-word-in-a-string/1449/2 "2018-07-03T01:16:05Z")

</div>

```auto
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();

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 3, 2018, 1:35am UTC](https://discourse.processing.org/t/how-to-separate-the-first-letter-of-each-word-in-a-string/1449/5 "2018-07-03T01:35:17Z")

</div>

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: 🕶

[https://Processing.org/reference/printArray\_.html](https://Processing.org/reference/printArray_.html)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 3, 2018, 1:38am UTC](https://discourse.processing.org/t/how-to-separate-the-first-letter-of-each-word-in-a-string/1449/6 "2018-07-03T01:38:48Z")

</div>

> [@nerdboybi](#):
>
> And is there any other way other than using the **split()** function?

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

> **[split() / Reference](https://processing.org/reference/split_.html)**
>
> The split() function breaks a String into pieces using a character or string as the delimiter. The delim parameter specifies the character or characters that mark the boundaries between …

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

> **[join() / Reference](https://processing.org/reference/join_.html)**
>
> Combines an array of Strings into one String, each separated by the character(s) used for the separator parameter. To join arrays of ints or floats, it's necessary to first convert them to Stri…

---

<div class="post-metadata">

**Author:** ![madscientist](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@madscientist](https://discourse.processing.org/u/madscientist)\
**Post date:** [July 3, 2018, 1:46am UTC](https://discourse.processing.org/t/how-to-separate-the-first-letter-of-each-word-in-a-string/1449/7 "2018-07-03T01:46:53Z")

</div>

You could do this:

```auto
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();

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 3, 2018, 2:24am UTC](https://discourse.processing.org/t/how-to-separate-the-first-letter-of-each-word-in-a-string/1449/11 "2018-07-03T02:24:59Z")

</div>

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()**: 🛋

[https://Processing.org/reference/splitTokens\_.html](https://Processing.org/reference/splitTokens_.html)

---

<div class="post-metadata">

**Author:** ![KAZ](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kaz/32/2402_2.png) [@KAZ](https://discourse.processing.org/u/KAZ)\
**Post date:** [July 3, 2018, 3:11am UTC](https://discourse.processing.org/t/how-to-separate-the-first-letter-of-each-word-in-a-string/1449/12 "2018-07-03T03:11:32Z")

</div>

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

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