-4

I try to split "." but it can not work, I get strings.length equals 0 .What's wrong with it?

String string = "11.12.1";
String[] strings = string.split(".");
Cenxui
  • 1,343
  • 1
  • 17
  • 25

1 Answers1

3

As string split takes an regex as argument, . is a wildcard for any character. Just escape it using a backslash (which you have to also escape for java with another one). Additionally, as Youcef Laidani pointed out, you have to call split on the string you just created, not something else:

string.split("\\.");
Leon
  • 2,926
  • 1
  • 25
  • 34
  • You are welcome. That `split` split always uses a regex is the only thing to keep in mind when using it, as it can have surprising effects if you don't. If you use the limit parameter, also remember, that you limit who many strings will be returned, not how many times the string will be split. Meaning if you set it to 1, the string will not be split at all. For more info you can also look at the [documentation](http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-). – Leon Jun 12 '16 at 13:03