【发布时间】:2013-05-19 00:54:42
【问题描述】:
我有以下代码
public static void main(String args[]) throws Exception
{
String mySite = "StackOverflow";
System.out.println(mySite.substring(0, 4));
}
这个打印的是Stac
根据我的理解,索引从 0 开始。
S<-0
t<-1
a<-2
c<-3
k<-4
当我指定我需要从索引 0 到索引 4 的字符串时,我希望返回 Stack,但它会返回 Stac。
当我查看 substring(int beginIndex, int endIndex) 源代码时,它显示以下内容。
* @param beginIndex the beginning index, inclusive.
* @param endIndex the ending index, exclusive.
有人能解释一下在这种情况下包容性和排他性是什么意思吗?为什么它没有变得更直观?有谁知道这种行为的任何具体原因?
【问题讨论】:
-
substring()根本不打印任何内容。请把你的标题改成有意义的东西。