用.(点)字符分割字符串java android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26749598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split String with .(dot) character java android
提问by
Hello friends i have string like
你好朋友,我有像这样的字符串
Android_a_b.pdf
i want to split it like Android_a_b
and pdf
我想它像分割Android_a_b
和pdf
i try following code like
我尝试以下代码
String s="Android_a_b.pdf";
String[] parts = s.split(".");
String part1 = parts[0];
String part2 = parts[1];
when i run above code it give me error like
当我运行上面的代码时,它给了我这样的错误
11-05 09:42:28.922: E/AndroidRuntime(8722): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at String part1 = parts[0];
line
在String part1 = parts[0];
行
any idea how can i solve it?
知道我该如何解决吗?
采纳答案by Ruchira Gayan Ranaweera
You need to escape .
using \
你需要逃避.
使用\
Eg:
例如:
String s="Android_a_b.pdf";
String[] parts = s.split("\."); // escape .
String part1 = parts[0];
String part2 = parts[1];
Now it will split by .
现在它将分裂为 .
Split(regex)in Java
斯普利特(正则表达式)在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.
围绕给定正则表达式的匹配拆分此字符串。
此方法的工作方式就像通过使用给定表达式和零限制参数调用双参数 split 方法一样。因此,结果数组中不包含尾随空字符串。
Keep in mind that
请记住
Parameters: regex - the delimiting regular expression
参数: regex - 分隔正则表达式