Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
Grammar.
Source Link
Mateen Ulhaq

Just useUse the appropriately named method: String#split().

Note that this takessplit's argument is assumed to be a regular expression, so remember to escape special characters if necessary.

SoFor instance, if you want to split on e.g.a period/dot . which(which means "any character" in regex), use either backslash \ to escape the individual special character like so split("\\."), or use character class [] to represent literal character(s) like so split("[.]"), or use Pattern#quote() to escape the entire string like so split(Pattern.quote(".")).

Just use the appropriately named method: String#split().

Note that this takes a regular expression, so remember to escape special characters if necessary.

So, if you want to split on e.g. period/dot . which means "any character" in regex, use either backslash \ to escape the individual special character like so split("\\."), or use character class [] to represent literal character(s) like so split("[.]"), or use Pattern#quote() to escape the entire string like so split(Pattern.quote(".")).

Use the appropriately named method String#split().

Note that split's argument is assumed to be a regular expression, so remember to escape special characters if necessary.

For instance, to split on a period/dot . (which means "any character" in regex), use either backslash \ to escape the individual special character like so split("\\."), or use character class [] to represent literal character(s) like so split("[.]"), or use Pattern#quote() to escape the entire string like so split(Pattern.quote(".")).

Updated links as per current state of technology.
Source Link
BalusC

Just use the appropriateappropriately named method: String#split()String#split().

Note that this takes a regular expressionregular expression, so remember to escape special charactersspecial characters if necessary.

therethere are 12 characters with special meanings: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), and the opening square bracket [, the opening curly brace {, These special characters are often called "metacharacters".

So, if you want to split on e.g. period/dot . which means "any characterany character" in regex, use either backslash \backslash \ to escape the individual special character like so split("\\."), or use character class []character class [] to represent literal character(s) like so split("[.]"), or use Pattern#quote()Pattern#quote() to escape the entire string like so split(Pattern.quote(".")).

String[] parts = string.split(Pattern.quote(".")); // Split on periodthe exact string.

To test beforehand if the string contains certain character(s), just use String#contains()String#contains().

Note, this does not take a regular expression. For that, use String#matches()String#matches() instead.

If you'd like to retain the split character in the resulting parts, then make use of positive lookaroundpositive lookaround. In case you want to have the split character to end up in left hand side, use positive lookbehind by prefixing ?<= group on the pattern.

Just use the appropriate method: String#split().

Note that this takes a regular expression, so remember to escape special characters if necessary.

there are 12 characters with special meanings: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), and the opening square bracket [, the opening curly brace {, These special characters are often called "metacharacters".

So, if you want to split on e.g. period/dot . which means "any character" in regex, use either backslash \ to escape the individual special character like so split("\\."), or use character class [] to represent literal character(s) like so split("[.]"), or use Pattern#quote() to escape the entire string like so split(Pattern.quote(".")).

String[] parts = string.split(Pattern.quote(".")); // Split on period.

To test beforehand if the string contains certain character(s), just use String#contains().

Note, this does not take a regular expression. For that, use String#matches() instead.

If you'd like to retain the split character in the resulting parts, then make use of positive lookaround. In case you want to have the split character to end up in left hand side, use positive lookbehind by prefixing ?<= group on the pattern.

Just use the appropriately named method: String#split().

Note that this takes a regular expression, so remember to escape special characters if necessary.

there are 12 characters with special meanings: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), and the opening square bracket [, the opening curly brace {, These special characters are often called "metacharacters".

So, if you want to split on e.g. period/dot . which means "any character" in regex, use either backslash \ to escape the individual special character like so split("\\."), or use character class [] to represent literal character(s) like so split("[.]"), or use Pattern#quote() to escape the entire string like so split(Pattern.quote(".")).

String[] parts = string.split(Pattern.quote(".")); // Split on the exact string.

To test beforehand if the string contains certain character(s), just use String#contains().

Note, this does not take a regular expression. For that, use String#matches() instead.

If you'd like to retain the split character in the resulting parts, then make use of positive lookaround. In case you want to have the split character to end up in left hand side, use positive lookbehind by prefixing ?<= group on the pattern.

Bounty Awarded with 250 reputation awarded by Matthieu
fixed spelling
Source Link
shanethehat

NoNote, this does not take a regular expression. For that, use String#matches() instead.

No, this does not take a regular expression. For that, use String#matches() instead.

Note, this does not take a regular expression. For that, use String#matches() instead.

Add String#matches() hint.
Source Link
BalusC
Loading
Integrate comments in answer.
Source Link
BalusC
Loading
Clarified more.
Source Link
BalusC
Loading
Links.
Source Link
BalusC
Loading
Removed italics as it seems to render badly on some machines. Added Pattern#quote() hint. Updated Javadocs to Java 8.
Source Link
BalusC
Loading
improved
Source Link
BalusC
Loading
Source Link
BalusC
Loading
lang-java