如果端索引不超出界限,则返回string.length()的子字符串方法?
基本上是标题。
String text = "abcdef";
String pattern = "defg";
int start = 3;
int end = 3 + pattern.length();
System.out.println(text.substring(start,end));
现在,这会从界限中引出索引,但是有些API可以解决此问题。 现在我只是在做
if (end > text.length()){
end = text.length();
}
Basically the title.
String text = "abcdef";
String pattern = "defg";
int start = 3;
int end = 3 + pattern.length();
System.out.println(text.substring(start,end));
Now that throws index out of bounds error but is there some API that can work around this.
Right now I'm just doing
if (end > text.length()){
end = text.length();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类似的方法,但使用Math.min以较短的方式处理它:
Similar approach but with Math.min to tackle it in a shorter way: