Introduction

Splitting a Strings is common operation. This short tutorial will show some of the ways to complete is operation.

String.split()

The most common way to split a string is using the split() method from the String class. It splits the given String based on the delimiter and returns an array for Strings.

Let’s try a few examples. First we’ll start with splitting by a common:

Split By a Whitespace:

Split By a Period:

String.split() with Limits

You can limit the size of the array return by passing a limit value to the split() method.

Let’s try a few examples. First we’ll return an array with two element:

Split returns array with 3 elements:

Conclusion

This tutorial cover different ways you can split a string in Java. You can find more tutorials on Java here

The source examples can be found here on GitHub.