gpt4 book ai didi

java - 用.(点)字符分割字符串java android

转载 作者:IT老高 更新时间:2023-10-28 20:48:54 25 4
gpt4 key购买 nike

你好 friend ,我有类似的字符串

Android_a_b.pdf

我想像 Android_a_bpdf

一样拆分它

我试试下面的代码

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com