Split String with。(dot)character java android

Split String with .(dot) character java android

本问题已经有最佳答案,请猛点这里访问。

你好,朋友们,我有一根绳子

1
Android_a_b.pdf

我想像Android_a_bpdf那样把它分开

我试着像这样遵循代码

1
2
3
4
String s="Android_a_b.pdf";
String[] parts = s.split(".");
String part1 = parts[0];  
String part2 = parts[1];

当我运行上面的代码时,它会给我如下的错误

1
11-05 09:42:28.922: E/AndroidRuntime(8722): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0

String part1 = parts[0];线

我知道怎么解决吗?


你需要用\来逃避.

如:

1
2
3
4
String s="Android_a_b.pdf";
String[] parts = s.split("\\."); // escape .
String part1 = parts[0];
String part2 = parts[1];

现在它将被.分割

Java中拆分(regex)

Splits this string around matches of the given regular expression.

This method works as if by invoking the two-argument split method with
the given expression and a limit argument of zero. Trailing empty
strings are therefore not included in the resulting array.

记住

Parameters:
regex - the delimiting regular expression