gpt4 book ai didi

java - 尝试从长字符串中获取字符串时出现 StringIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-11-29 19:05:58 25 4
gpt4 key购买 nike

我试图从 Firebase URL 的长字符串中获取字符串

"https://firebasestorage.googleapis.com/v0/b/No-manworld-3577.appspot.com/o/Contacts%2F1510361061636_Julien_Vcf?alt=media&token=c0bff20d-d115-4fef-b58c-4c7ffaef4296"

现在,如果您注意到上面的字符串中名称 Julien 前后都有下划线。我正在尝试获取该名称,但我正在获取

java.lang.StringIndexOutOfBoundsException: String index out of range: -1

这是我的代码

String s="https://firebasestorage.googleapis.com/v0/b/No-manworld-3577.appspot.com/o/Contacts%2F1510361061636_Julien_Vcf?alt=media&token=c0bff20d-d115-4fef-b58c-4c7ffaef4296";

String newName=s.substring(s.indexOf("_")+1, s.indexOf("_"));
System.out.println(newName);

最佳答案

正如我在评论中所说,使用子字符串时,第一个数字必须小于第二个数字。

在您的例子中,您正在使用 x + 1x 调用子字符串。 x + 1 > x 因此子字符串失败,xs.indexOf("_")

我知道您正在尝试获取 second indexOf _

这是在您的情况下会产生 Julien 的代码:

String s = "...";

int start = s.indexOf("_") + 1;
int end = s.indexOf("_", start);

// name will hold the content of s between the first two `_`s, assuming they exist.
String name = s.substring(start, end);

关于java - 尝试从长字符串中获取字符串时出现 StringIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47233375/

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