你好 friend ,我有类似的字符串
Android_a_b.pdf
我想像 Android_a_b
和 pdf
我试试下面的代码
String s="Android_a_b.pdf";
String[] parts = s.split(".");
String part1 = parts[0];
String part2 = parts[1];
当我运行上面的代码时,它给了我类似的错误
11-05 09:42:28.922: E/AndroidRuntime(8722): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
在 String part1 = parts[0];
行
知道怎么解决吗?
最佳答案
您需要使用 \
.
例如:
String s="Android_a_b.pdf";
String[] parts = s.split("\\."); // escape .
String part1 = parts[0];
String part2 = parts[1];
现在它将被 分割。
Split(regex)在 Java
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
关于java - 用.(点)字符分割字符串java android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26749598/