Java如何實(shí)現(xiàn)substring方法

小編這次要給大家分享的是Java如何實(shí)現(xiàn)substring方法,文章內(nèi)容豐富,感興趣的小伙伴可以來(lái)了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

創(chuàng)新互聯(lián)公司專業(yè)提供成都主機(jī)托管四川主機(jī)托管成都服務(wù)器托管四川服務(wù)器托管,支持按月付款!我們的承諾:貴族品質(zhì)、平民價(jià)格,機(jī)房位于中國(guó)電信/網(wǎng)通/移動(dòng)機(jī)房,成都機(jī)柜租用服務(wù)有保障!

substring實(shí)現(xiàn)原理

String是Java中一個(gè)比較基礎(chǔ)的類,每一個(gè)開發(fā)人員都會(huì)經(jīng)常接觸到。而且,String也是面試中經(jīng)常會(huì)考的知識(shí)點(diǎn)。String有很多方法,有些方法比較常用,有些方法不太常用。今天要介紹的subString就是一個(gè)比較常用的方法,而且圍繞subString也有很多面試題。

substring(int beginIndex, int endIndex)方法在不同版本的JDK中的實(shí)現(xiàn)是不同的。了解他們的區(qū)別可以幫助你更好的使用他。為簡(jiǎn)單起見,后文中用substring()代表substring(int beginIndex, int endIndex)方法。

substring()的作用

substring(int beginIndex, int endIndex)方法截取字符串并返回其[beginIndex,endIndex-1]范圍內(nèi)的內(nèi)容。s

String x = "abcdef";x = x.substring(1,3);System.out.println(x);

輸出內(nèi)容:

bc

調(diào)用substring時(shí)發(fā)生了什么?

你可能知道,因?yàn)閤是不可變的,當(dāng)使用x.substring(1,3)對(duì)x賦值的時(shí)候,它會(huì)指向一個(gè)全新的字符串:

Java如何實(shí)現(xiàn)substring方法

然而,這個(gè)圖不是完全正確的表示堆中發(fā)生的事情。因?yàn)樵趈dk6 和 jdk7中調(diào)用substring時(shí)發(fā)生的事情并不一樣。

JDK 6中的subString

tring是通過(guò)字符數(shù)組實(shí)現(xiàn)的。在jdk 6 中,String類包含三個(gè)成員變量:char value[], int offset,int count,他們分別用來(lái):存儲(chǔ)真正的字符數(shù)組、存儲(chǔ)數(shù)組的第一個(gè)位置索引、存儲(chǔ)字符串中包含的字符個(gè)數(shù)。

當(dāng)調(diào)用substring方法的時(shí)候,會(huì)創(chuàng)建一個(gè)新的string對(duì)象,但是這個(gè)string的值仍然指向堆中的同一個(gè)字符數(shù)組。這兩個(gè)對(duì)象中只有count和offset 的值是不同的。

Java如何實(shí)現(xiàn)substring方法

源碼

//JDK 6
String(int offset, int count, char value[]) {
  this.value = value;
  this.offset = offset;
  this.count = count;
}

public String substring(int beginIndex, int endIndex) {
  //check boundary
  return new String(offset + beginIndex, endIndex - beginIndex, value);
}

存在的問(wèn)題

如果有一個(gè)很長(zhǎng)的字符串,但是你只需要使用很短的一段,于是你使用substring進(jìn)行切割,但是由于你實(shí)際上引用了整個(gè)字符串,這個(gè)很長(zhǎng)的字符串無(wú)法被回收。往小了說(shuō),造成了存儲(chǔ)空間的浪費(fèi),往大了說(shuō),可能造成內(nèi)存泄漏。這個(gè)問(wèn)題已經(jīng)被官方記錄在Java Bug Database里面了:

Java如何實(shí)現(xiàn)substring方法

相應(yīng)的解決辦法:

s1 = s1.substring(x,y) + "";

JDK 7 中的substring

上述問(wèn)題在JDK 7中得到了解決。JDK 7中,substring方法會(huì)在堆中創(chuàng)建一個(gè)新的數(shù)組。

Java如何實(shí)現(xiàn)substring方法

源碼

  //JDK 7

  /**
   * Allocates a new {@code String} that contains characters from a subarray
   * of the character array argument. The {@code offset} argument is the
   * index of the first character of the subarray and the {@code count}
   * argument specifies the length of the subarray. The contents of the
   * subarray are copied; subsequent modification of the character array does
   * not affect the newly created string.
   *
   * @param value Array that is the source of characters
   * @param offset The initial offset
   * @param count The length
   * @throws IndexOutOfBoundsException If the {@code offset} and {@code count} arguments index
   *                  characters outside the bounds of the {@code value} array
   */
  public String(char value[], int offset, int count) {
    //check boundary
    this.value = Arrays.copyOfRange(value, offset, offset + count);
  }

  /**
   * Returns a string that is a substring of this string. The
   * substring begins at the specified {@code beginIndex} and
   * extends to the character at index {@code endIndex - 1}.
   * Thus the length of the substring is {@code endIndex-beginIndex}.
   * <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.
   * @throws IndexOutOfBoundsException if the
   *                  {@code beginIndex} is negative, or
   *                  {@code endIndex} is larger than the length of
   *                  this {@code String} object, or
   *                  {@code beginIndex} is larger than
   *                  {@code endIndex}.
   */
  public String substring(int beginIndex, int endIndex) {
    //check boundary
    int subLen = endIndex - beginIndex;
    return ((beginIndex == 0) && (endIndex == value.length)) &#63;
        this :
        new String(value, beginIndex, subLen);
  }

看完這篇關(guān)于Java如何實(shí)現(xiàn)substring方法的文章,如果覺(jué)得文章內(nèi)容寫得不錯(cuò)的話,可以把它分享出去給更多人看到。

網(wǎng)站名稱:Java如何實(shí)現(xiàn)substring方法
鏈接分享:http://muchs.cn/article48/ihiphp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、網(wǎng)站策劃、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站排名、定制網(wǎng)站、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)