Skip Top Navigation Bar

Splitting a String of Characters with split() - Example 1

Snippet: Splitting a String of Characters with split()

Explanation

This snippet introduces the split() method, which breaks a String into an array of substrings using a delimiter. Here, a single space " " is used as the delimiter, so each word becomes one element of the resulting String[] array.

The famous pangram "the quick brown fox jumps over the lazy dog" contains 9 words, so words.length is 9. The first word is accessed with words[0] and the last with words[words.length - 1] — a common pattern for accessing the last element of any array.

What You Can Do

Try splitting the string with a different delimiter, like "o". How many parts do you get, and what does each part look like? Are they still words?

You can also try printing all the words in the array one by one. Can you use words.length to know how many times to repeat the print?

Run it and observe the output!

Example List

Additional Resources