A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© android-liu 中级黑马   /  2015-3-11 20:00  /  1051 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. String str="ddd";
  2. String ss=str.substring(3);
  3. System.out.println(ss);
复制代码

大家帮我看看字符串角标为什么没有出越界异常

4 个回复

倒序浏览
String类源码实现
  1. /**
  2.      * Returns a string that is a substring of this string. The
  3.      * substring begins with the character at the specified index and
  4.      * extends to the end of this string. <p>
  5.      * Examples:
  6.      * <blockquote><pre>
  7.      * "unhappy".substring(2) returns "happy"
  8.      * "Harbison".substring(3) returns "bison"
  9.      * "emptiness".substring(9) returns "" (an empty string)
  10.      * </pre></blockquote>
  11.      *
  12.      * @param      beginIndex   the beginning index, inclusive.
  13.      * @return     the specified substring.
  14.      * @exception  IndexOutOfBoundsException  if
  15.      *             {@code beginIndex} is negative or larger than the
  16.      *             length of this {@code String} object.
  17.      */
  18.     public String substring(int beginIndex) {
  19.         if (beginIndex < 0) {
  20.             throw new StringIndexOutOfBoundsException(beginIndex);
  21.         }
  22.         int subLen = value.length - beginIndex;
  23.         if (subLen < 0) {
  24.             throw new StringIndexOutOfBoundsException(subLen);
  25.         }
  26.         return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
  27.     }
复制代码
回复 使用道具 举报
不知道你有没有听说过这样一句话:字符串的截取是包括头不包括尾的
回复 使用道具 举报 1 0
ddd是 0 1 2没有3  更没有3以后的  。。。。subString (int index)是从index开始到length 你3已然越界了 少年
回复 使用道具 举报
ccl|r 来自手机 中级黑马 2015-3-11 23:28:21
报纸
在基础班还没上到这
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马