public String substring(int beginIndex, int endIndex)(Java)
2016-08-27 13:00
555 查看
基础不牢,地动山摇。今天开始写一些基础Java卷子,写这篇博文原因是看到了这个题。
然后抱着认真学习的态度,默默地去敲了一下代码。确实是这个结果:
函数的源代码是这样的:
虽然注释已经写得很清楚了,但是我们还是关注一下最关键的这句话吧:
return ((beginIndex == 0) && (endIndex == count)) ? this :
new String(offset + beginIndex, endIndex - beginIndex, value);
这句代码表明我们实际上是获得了一个新的字符串,用下面这种构造方式:
看到这里,不得不说一下 value offset 和count这三个成员变量了,它们的解释如下:
也就是说,我们的String其实归根结底也是一个字符数组,但是不同的是,它有两个比较给力的属性,一个是offset另一个是count,这两个属性一个标记起始的位置,一个标记字符的个数,也就是字符串的长度。这就不难理解我们使用substring(1,3)时,其中的(beginIndex,endIndex)是“左闭右开”的了。
首先,调用这个函数的时候,传入了beginIndex和endIndex。随即函数内部调用new String(offset + beginIndex, endIndex - beginIndex, value)将offset变成了当前下标,count变成了endIndex-beginIndex,然后存储的区域仍然不变。
然后,在此题中,1传进来后,offset+1即向右偏移了一下,到了2的位置。(所以这里的规律是,前面是闭区间,正好可以对应数组下标)但接下来就有所不同了,传入了一个字符串的“长度”也就是3-1=2。那么这个时候,从2这个数开始,长度为2的串,就是“23”了。
最后,我想说,多读源码是有助于对于程序的理解和书写的。希望小伙伴们一起来,点击函数,按下F3!
然后抱着认真学习的态度,默默地去敲了一下代码。确实是这个结果:
函数的源代码是这样的:
/** * Returns a new string that is a substring of this string. The * substring begins at the specified <code>beginIndex</code> and * extends to the character at index <code>endIndex - 1</code>. * Thus the length of the substring is <code>endIndex-beginIndex</code>. * <p> * Examples: * <blockquote><pre> * "hamburger".substring(4, 8) returns "urge" * "smiles".substring(1, 5) returns "mile" * </pre></blockquote> * * @param beginIndex the beginning index, inclusive. * @param endIndex the ending index, exclusive. * @return the specified substring. * @exception IndexOutOfBoundsException if the * <code>beginIndex</code> is negative, or * <code>endIndex</code> is larger than the length of * this <code>String</code> object, or * <code>beginIndex</code> is larger than * <code>endIndex</code>. */ public String substring(int beginIndex, int endIndex) { if (beginIndex < 0) { throw new StringIndexOutOfBoundsException(beginIndex); } if (endIndex > count) { throw new StringIndexOutOfBoundsException(endIndex); } if (beginIndex > endIndex) { throw new StringIndexOutOfBoundsException(endIndex - beginIndex); } return ((beginIndex == 0) && (endIndex == count)) ? this : new String(offset + beginIndex, endIndex - beginIndex, value); }
虽然注释已经写得很清楚了,但是我们还是关注一下最关键的这句话吧:
return ((beginIndex == 0) && (endIndex == count)) ? this :
new String(offset + beginIndex, endIndex - beginIndex, value);
这句代码表明我们实际上是获得了一个新的字符串,用下面这种构造方式:
// Package private constructor which shares value array for speed. String(int offset, int count, char value[]) { this.value = value; this.offset = offset; this.count = count; }
看到这里,不得不说一下 value offset 和count这三个成员变量了,它们的解释如下:
/** The value is used for character storage. */ private final char value[]; /** The offset is the first index of the storage that is used. */ private final int offset; /** The count is the number of characters in the String. */ private final int count;
也就是说,我们的String其实归根结底也是一个字符数组,但是不同的是,它有两个比较给力的属性,一个是offset另一个是count,这两个属性一个标记起始的位置,一个标记字符的个数,也就是字符串的长度。这就不难理解我们使用substring(1,3)时,其中的(beginIndex,endIndex)是“左闭右开”的了。
首先,调用这个函数的时候,传入了beginIndex和endIndex。随即函数内部调用new String(offset + beginIndex, endIndex - beginIndex, value)将offset变成了当前下标,count变成了endIndex-beginIndex,然后存储的区域仍然不变。
然后,在此题中,1传进来后,offset+1即向右偏移了一下,到了2的位置。(所以这里的规律是,前面是闭区间,正好可以对应数组下标)但接下来就有所不同了,传入了一个字符串的“长度”也就是3-1=2。那么这个时候,从2这个数开始,长度为2的串,就是“23”了。
最后,我想说,多读源码是有助于对于程序的理解和书写的。希望小伙伴们一起来,点击函数,按下F3!
相关文章推荐
- 关于java.lang.String中的public int lastIndexOf(String,int)
- JAVA String方法中public int indexOf(int ch)问题
- Java StringBuffer.substring(int start, int end)方法
- java慎用String.substring(int start, int end)
- java 中substring(int beginIndex, int endIndex) 的用法
- public String substring(int start, int end)
- String中subString(int start,int end) index的问题
- Java.substring(int beginIndex)的疑问?
- String数据类型substring(int beginIndex)
- Java中的String中的substring、indexof()、lastindexof()和split
- Method public java.lang.String org.hibernate.exception.NestableRuntimeException.getMessage(int
- js:string.substring(int start,int end)
- 更改java.util.List中的对象引用String java.util.List.set(int index, String element)
- Java String.substring(int, int) 的使用
- java String substring(int beginIndex,int endIndex)
- Java中的String中的substring、indexof()、lastindexof()和split
- public int indexOf(String str)(Java)
- substringToIndex 'Int' is not convertible to 'String.Index'
- String的indexOf(),lastIndexOf(),substring(int x),substring(int x , int y)方法 StringBuffer
- Err:The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List or array