Split multiple delimiters in Java

Split multiple delimiters in Java

Try with



Use String.split() with multiple delimiters

The split method takes as argument a regular expression so, to use multiple delimiters, you need to input a regular expression separated by the OR regex operator or using a character class (only if the delimiters are single characters).

Using the OR operator:

String delimiters = \t|,|;|\.|\?|!|-|:|@|\[|\]|\(|\)|\{|\}|_|\*|/;

Using the character class:

String delimiters = [-\t,;.?!:@\[\](){}_*/];

As you can see some of the characters must be escaped as they are regex metacharacters.

Split multiple delimiters in Java

Leave a Reply

Your email address will not be published. Required fields are marked *