在Scala REPL中,当在太短的字符串上调用子字符串以至于无法剪切时,它会按预期将索引超出范围:
scala> "123".substring(0,6)
java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.substring(String.java:1963)
... 32 elided
但是在Scala.js中运行上述操作不会引发异常:
"123".substring(0,6)
// returns "123"
为什么会这样?
答案 0 :(得分:3)
以下是an almost identical question的维护者答案:
也就是说,不,我们不会保持一致,因为
StringIndexOutOfBounds
是Scala.js中未定义的行为。
您可以检查the documentation of the semantics of Scala.js来确认此主张,但是与substring
有关的这种特殊情况以前使我感到困惑,因此应该在该页面上更具体地注明。