The split() method in Java does not work on a dot (.) [duplicate] The split() method in Java does not work on a dot (.) [duplicate] java java

The split() method in Java does not work on a dot (.) [duplicate]


java.lang.String.split splits on regular expressions, and . in a regular expression means "any character".

Try temp.split("\\.").


The documentation on split() says:

Splits this string around matches of the given regular expression.

(Emphasis mine.)

A dot is a special character in regular expression syntax. Use Pattern.quote() on the parameter to split() if you want the split to be on a literal string pattern:

String[] words = temp.split(Pattern.quote("."));


Try:

String words[]=temp.split("\\.");

The method is:

String[] split(String regex) 

"." is a reserved char in regex