文章出處

在String中有兩個substring()函數,如下:

一:String.substring(int start)

參數:

    start:要截取位置的索引

返回:

   從start開始到結束的字符串

例如:String str = "hello word!";
         System.out.println(str.substring(1));

         System.out.println(str.substring(3));

   System.out.println(str.substring(6));

將得到結果為:

         ello word!

         lo word!

         ord!

如果start大于字符串的長度將會拋出越界異常;

二:String.substring(int beginIndex, int endIndex)

參數:

  beginIndex 開始位置索引

      endIndex    結束位置索引

返回:

      從beginIndex位置到endIndex位置內的字符串

例如:String str = "hello word!";

         System.out.println(str.substring(1,4));

         System.out.println(str.substring(3,5));

   System.out.println(str.substring(0,4));

將得到結果為:

        ell

        lo 

        hell

如果startIndex和endIndex其中有越界的將會拋出越界異常。


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()