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

原文地址:https://www.cnblogs.com/LinQingYang/p/12683016.html

时间: 2024-08-24 17:07:50

JDK6和JDK7中String类下的substring方法的代码对比(仅贴代码,未详述)的相关文章

C#中String类的几个方法(IndexOf、LastIndexOf、Substring)

String.IndexOf String.IndexOf 方法 (Char, Int32, Int32)报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置.String.IndexOf(value, startIndex, count) 参数value:要查找的 Unicode 字符. startIndex:搜索起始位置. count:要检查的字符位置数.返回值(Int32):如果找到该字符,则为 value 的索引位置:否则如果未找到,则为 -1.

转:C#中String类的几个方法(IndexOf、LastIndexOf、Substring)

String.IndexOf String.IndexOf 方法 (Char, Int32, Int32)报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置.String.IndexOf(value, startIndex, count) 参数value:要查找的 Unicode 字符. startIndex:搜索起始位置. count:要检查的字符位置数.返回值(Int32):如果找到该字符,则为 value 的索引位置:否则如果未找到,则为 -1.

C#中string类的几个方法(indexof、lastindexof、substring)(转)

String.IndexOf String.IndexOf 方法 (Char, Int32, Int32) 报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置. String.IndexOf(value, startIndex, count) 参数 value:要查找的 Unicode 字符. startIndex:搜索起始位置. count:要检查的字符位置数. 返回值(Int32): 如果找到该字符,则为 value 的索引位置:否则如果未找到,则为

105、Java中String类之利用indexOf()方法判断子字符串是否存在

01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public static void main(String args[]) { String str = "helloworld"; // 字符串对象 if (str.indexOf("world") != -1) { // 能找到子字符串 System.out.println(

106、Java中String类之使用contains()方法判断子字符串是否存在

01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public static void main(String args[]) { String str = "helloworld"; // 字符串对象 if (str.contains("world")) { // 子字符串存在 System.out.println("

关于Java中String类的hashCode方法

首先来看一下String中hashCode方法的实现源码 1 public int hashCode() { 2 int h = hash; 3 if (h == 0 && value.length > 0) { 4 char val[] = value; 5 6 for (int i = 0; i < value.length; i++) { 7 h = 31 * h + val[i]; 8 } 9 hash = h; 10 } 11 return h; 12 } 在Stri

Java中String类(字符串操作)的10个常见问题和解决方法

注:来自百度搜索,还没有码一遍,一定要码!! 1. 字符串比较,使用 "==" 还是 equals() ?简单来说, "==" 判断两个引用的是不是同一个内存地址(同一个物理对象).而 equals 判断两个字符串的值是否相等.除非你想判断两个string引用是否同一个对象,否则应该总是使用 equals()方法.如果你了解 字符串的驻留 ( String Interning ) 则会更好地理解这个问题 2. 对于敏感信息,为何使用char[]要比String更好?

菜鸟译文(三)——JDK6和JDK7中substring()方法的对比

substring(int beginIndex, int endIndex)方法在JDK6和JDK7中是不同的.了解他们的区别可以让我们更好的使用这个方法.方便起见,以下用substring() 代替 substring(int beginIndex, int endIndex). 1. substring()做了什么? substring(int beginIndex, int endIndex)方法返回一个以beginIndex开头,以endIndex-1结尾的String对象. Stri

hadoop中Text类 与 java中String类的区别

hadoop 中 的Text类与java中的String类感觉上用法是相似的,但两者在编码格式和访问方式上还是有些差别的,要说明这个问题,首先得了解几个概念: 字符集: 是一个系统支持的所有抽象字符的集合.字符是各种文字和符号的总称,包括各国家文字.标点符号.图形符号.数字等.例如 unicode就是一个字符集,它的目标是涵盖世界上所有国家的文字和符号: 字符编码:是一套法则,使用该法则能够对自然语言的字符的一个集合(如字母表或音节表),与其他东西的一个集合(如号码或电脉冲)进行配对.即在符号集