zoukankan      html  css  js  c++  java
  • JDK6和JDK7中String类下的substring方法的代码对比(仅贴代码,未详述)

    jdk1.6版本String.java文件中关于substring的代码及描述

     1 /**
     2      * Returns a new 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</code> is negative or larger than the
    16      *             length of this <code>String</code> object.
    17      */
    18     public String substring(int beginIndex) {
    19     return substring(beginIndex, count);
    20     }
    21 
    22     /**
    23      * Returns a new string that is a substring of this string. The
    24      * substring begins at the specified <code>beginIndex</code> and
    25      * extends to the character at index <code>endIndex - 1</code>.
    26      * Thus the length of the substring is <code>endIndex-beginIndex</code>.
    27      * <p>
    28      * Examples:
    29      * <blockquote><pre>
    30      * "hamburger".substring(4, 8) returns "urge"
    31      * "smiles".substring(1, 5) returns "mile"
    32      * </pre></blockquote>
    33      *
    34      * @param      beginIndex   the beginning index, inclusive.
    35      * @param      endIndex     the ending index, exclusive.
    36      * @return     the specified substring.
    37      * @exception  IndexOutOfBoundsException  if the
    38      *             <code>beginIndex</code> is negative, or
    39      *             <code>endIndex</code> is larger than the length of
    40      *             this <code>String</code> object, or
    41      *             <code>beginIndex</code> is larger than
    42      *             <code>endIndex</code>.
    43      */
    44     public String substring(int beginIndex, int endIndex) {
    45     if (beginIndex < 0) {
    46         throw new StringIndexOutOfBoundsException(beginIndex);
    47     }
    48     if (endIndex > count) {
    49         throw new StringIndexOutOfBoundsException(endIndex);
    50     }
    51     if (beginIndex > endIndex) {
    52         throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
    53     }
    54     return ((beginIndex == 0) && (endIndex == count)) ? this :
    55         new String(offset + beginIndex, endIndex - beginIndex, value);
    56     }

    jdk1.7版本String.java文件中关于substring的代码及描述

     1 /**
     2      * Returns a new 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</code> is negative or larger than the
    16      *             length of this <code>String</code> 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     }
    28 
    29     /**
    30      * Returns a new string that is a substring of this string. The
    31      * substring begins at the specified <code>beginIndex</code> and
    32      * extends to the character at index <code>endIndex - 1</code>.
    33      * Thus the length of the substring is <code>endIndex-beginIndex</code>.
    34      * <p>
    35      * Examples:
    36      * <blockquote><pre>
    37      * "hamburger".substring(4, 8) returns "urge"
    38      * "smiles".substring(1, 5) returns "mile"
    39      * </pre></blockquote>
    40      *
    41      * @param      beginIndex   the beginning index, inclusive.
    42      * @param      endIndex     the ending index, exclusive.
    43      * @return     the specified substring.
    44      * @exception  IndexOutOfBoundsException  if the
    45      *             <code>beginIndex</code> is negative, or
    46      *             <code>endIndex</code> is larger than the length of
    47      *             this <code>String</code> object, or
    48      *             <code>beginIndex</code> is larger than
    49      *             <code>endIndex</code>.
    50      */
    51     public String substring(int beginIndex, int endIndex) {
    52         if (beginIndex < 0) {
    53             throw new StringIndexOutOfBoundsException(beginIndex);
    54         }
    55         if (endIndex > value.length) {
    56             throw new StringIndexOutOfBoundsException(endIndex);
    57         }
    58         int subLen = endIndex - beginIndex;
    59         if (subLen < 0) {
    60             throw new StringIndexOutOfBoundsException(subLen);
    61         }
    62         return ((beginIndex == 0) && (endIndex == value.length)) ? this
    63                 : new String(value, beginIndex, subLen);
    64     }

    点我,免费下载1.6、1.7版本的String类,仔细查看substring方法的异同。

    链接:https://pan.baidu.com/s/13PQPcdeiD1J3Cj4VDJov5A
    提取码:pg63

  • 相关阅读:
    开源软件、自由软件及免费软件的区别 2015-04-13 22:50 45人阅读 评论(0) 收藏
    Linux中fork()函数详解 2015-04-13 22:48 30人阅读 评论(0) 收藏
    ubuntu系统文件夹目录说明 分类: Ubuntu学习笔记 2015-04-13 21:21 49人阅读 评论(0) 收藏
    gdb使用笔记 2015-04-11 19:55 32人阅读 评论(0) 收藏
    Vim 使用笔记 2015-04-10 19:50 51人阅读 评论(0) 收藏
    access_ok()
    linux内核驱动中_IO, _IOR, _IOW, _IOWR 宏的用法与解析(引用)
    编译andriod源码出错:java.lang.UnsupportedClassVersionError: com/google/doclava/Doclava : Unsupported
    Linux内核树的建立-基于ubuntu系统
    sysfs接口整理
  • 原文地址:https://www.cnblogs.com/LinQingYang/p/12683016.html
Copyright © 2011-2022 走看看