java中substring的用法,javasubstring用法

來源:互聯網
上載者:User

java中substring的用法,javasubstring用法

substring


1.public String substring(int beginIndex)。
     返回一個新的字串,它是此字串的一個子字串。該子字串始於指定索引處的字元,一直到此字串末尾。


     參數:
  beginIndex - 開始處的索引(包括)。


  返回:
  指定的子字串。


      例如:
  "unhappy".substring(2) returns "happy"
  "Harbison".substring(3) returns "bison"
  "emptiness".substring(9) returns "" (an empty string)

 2.public String substring(int beginIndex,int length)。
     返回一個新字串,它是此字串的一個子字串。該子字串從指定的 beginIndex 處開始, length:表示子字串的長度。


     參數:
  beginIndex - 開始處的索引(包括)。
  endindex 結尾處索引(不包括)。


  返回:
  指定的子字串。


     樣本:
  "hamburger".substring(4,8) returns "urge"
  "smiles".substring(1,5) returns "mile"

3.

     <script type="text/javascript">

  var str="Hello world!"  document.write(str.substring(1,3));

  </script>

 

    上面返回字串:"ell";  str.substring(1,1) //返回e

 

    str.substring(1) //返回"ello world";

 

    還有此函數中會出現奇怪的現象,當出現str.substring(5,0);  這又是怎麼回事,不過返回的是"hello",  str.substring(5,1) //    返 回"ello",截去了第一位,返回餘下的.

 

   可見substring(start,end),可以有不同的說明,即start可以是要返回的長度,end是所要去掉的多少個字元(從首位開始).

 

   在JS中,substr(start,length),用得較方便.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.