I've written the following code:
String[] arr = ((String) "asd.asd").split(".");
and arr=[]
. Why?
I've written the following code:
String[] arr = ((String) "asd.asd").split(".");
and arr=[]
. Why?
split
takes a regular expression as an argument. "." in regular means "any character".
Instead, use:
String[] arr = "asd.asd".split("\\.");
The backslashes escape the special meaning of the "." character in a regular expression.
split()
accepts a regex. you should escape the .
use "\\."
. In regex .
is a special character (Meta character) which means match any character.
You must double escape the .
, otherwise the regular expression represents it as "any character".
Also, you don't need to cast "asd.asd" as String
.
String[] arr = "asd.asd".split("\\.");
Because '.' is a special character. You need to escape it by writing it like this '\\.'