java中字串截取,調用substring()方法__java

來源:互聯網
上載者:User

substring() 方法返回字串的子字串。在java中 substring()方法有兩種用法,

第一種

public String substring(int beginIndex)

第二種

public String substring(int beginIndex, int endIndex)

參數的意思

beginIndex -- 起始索引(包括)。

endIndex -- 結束索引(不包括)。

第一種:返回一個新的字串,它是此字串的一個子字串。該子字串從指定索引處的字元開始,直到此字串末尾。
第二種:返回一個新字串,它是此字串的一個子字串。該子字串從指定的 beginIndex 處開始,直到索引 endIndex - 1 處的字元。因此,該子字串的長度為 endIndex-beginIndex。 範例程式碼:

public static void main(String[] args) {String Str = new String("hello wrold");         System.out.print("傳回值 :" );        System.out.println(Str.substring(4) );         System.out.print("傳回值 :" );        System.out.println(Str.substring(4, 10) );}
運行結果:

傳回值 :o wrold
傳回值 :o wrol


最後的最後  :各位看官,看完的覺得有用又不嫌麻煩的就給個贊或者給個評論唄,嫌麻煩的也請麻煩一下給個讚唄,想踩一腳的,手下留情,碼字不易,且看且珍惜,(我的大刀已經饑渴難耐了)



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.