Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
Active reading [<https://en.wiktionary.org/wiki/delimiter#Noun> <https://en.wiktionary.org/wiki/reuse#Verb>].
Source Link
Peter Mortensen

There are only two methods you really need to consider.

Use String.split iffor a one character delimeter-character delimiter or you don't care about performance

If performance is not an issue, or if the delimeterdelimiter is a single character that is not a regular expression special character (i.e., not one of .$|()[{^?*+\) then you can use String.split.

String[] results = input.split(",");

The split method has an optimization to avoid using a regular expression if the delimeter is a single character and not in the above list. Otherwise, it has to compile a regular expression, and this is not ideal.

Use Pattern.split and precompile the pattern if using a complex delimeterdelimiter and you care about performance.

If performance is an issue, and your delimeterdelimiter is not one of the above, you should pre-compile a regular expression pattern which you can then re-usereuse.

// Save this somewhere
Pattern pattern = Pattern.compile("[,;:]");

/// ... later
String[] results = pattern.split(input);

This last option still creates a new Matcher object. You can also cache this object and reset it for each input for maximum performance, but that is somewhat more complicated and not thread-safe.

There are only two methods you really need to consider.

Use String.split if a one character delimeter or you don't care about performance

If performance is not an issue, or if the delimeter is a single character that is not a regular expression special character (i.e., not one of .$|()[{^?*+\) then you can use String.split.

String[] results = input.split(",");

The split method has an optimization to avoid using a regular expression if the delimeter is a single character and not in the above list. Otherwise, it has to compile a regular expression and this is not ideal.

Use Pattern.split and precompile the pattern if using a complex delimeter and you care about performance

If performance is an issue and your delimeter is not one of the above, you should pre-compile a regular expression pattern which you can then re-use.

// Save this somewhere
Pattern pattern = Pattern.compile("[,;:]");

/// ... later
String[] results = pattern.split(input);

This last option still creates a new Matcher object. You can also cache this object and reset it for each input for maximum performance, but that is somewhat more complicated and not thread-safe.

There are only two methods you really need to consider.

Use String.split for a one-character delimiter or you don't care about performance

If performance is not an issue, or if the delimiter is a single character that is not a regular expression special character (i.e., not one of .$|()[{^?*+\) then you can use String.split.

String[] results = input.split(",");

The split method has an optimization to avoid using a regular expression if the delimeter is a single character and not in the above list. Otherwise, it has to compile a regular expression, and this is not ideal.

Use Pattern.split and precompile the pattern if using a complex delimiter and you care about performance.

If performance is an issue, and your delimiter is not one of the above, you should pre-compile a regular expression pattern which you can then reuse.

// Save this somewhere
Pattern pattern = Pattern.compile("[,;:]");

/// ... later
String[] results = pattern.split(input);

This last option still creates a new Matcher object. You can also cache this object and reset it for each input for maximum performance, but that is somewhat more complicated and not thread-safe.

Source Link
rghome

There are only two methods you really need to consider.

Use String.split if a one character delimeter or you don't care about performance

If performance is not an issue, or if the delimeter is a single character that is not a regular expression special character (i.e., not one of .$|()[{^?*+\) then you can use String.split.

String[] results = input.split(",");

The split method has an optimization to avoid using a regular expression if the delimeter is a single character and not in the above list. Otherwise, it has to compile a regular expression and this is not ideal.

Use Pattern.split and precompile the pattern if using a complex delimeter and you care about performance

If performance is an issue and your delimeter is not one of the above, you should pre-compile a regular expression pattern which you can then re-use.

// Save this somewhere
Pattern pattern = Pattern.compile("[,;:]");

/// ... later
String[] results = pattern.split(input);

This last option still creates a new Matcher object. You can also cache this object and reset it for each input for maximum performance, but that is somewhat more complicated and not thread-safe.

lang-java