Java - String lastIndexOf() 方法



宣告

以下是Java String lastIndexOf()方法的宣告

public int lastIndexOf(int ch)

引數

ch − 這是字元(Unicode 程式碼點)的值。

返回值

此方法返回此物件表示的字元序列中字元最後一次出現的索引,如果字元不存在則返回 -1。

異常

Java - String lastIndexOf(int ch, int fromIndex) 方法

描述

Java String lastIndexOf(int ch, int fromIndex) 方法返回此字串中指定字元最後一次出現的索引,從指定的索引開始向後搜尋。

宣告

以下是Java String lastIndexOf()方法的宣告

public int lastIndexOf(int ch, int fromIndex)

引數

  • ch − 這是字元(Unicode 程式碼點)的值。

  • fromIndex − 這是開始搜尋的索引。如果它大於或等於此字串的長度,則其效果與等於此字串長度減 1 相同:可以搜尋整個字串。如果它為負數,則其效果與等於 -1 相同:返回 -1。

返回值

此方法返回此物件表示的字元序列中小於或等於 fromIndex 的字元最後一次出現的索引,如果字元在該點之前不存在則返回 -1。

異常

Java - String lastIndexOf(String str) 方法

描述

java.lang.String.lastIndexOf(String str) 方法返回此字串中指定子字串最右邊出現位置的索引。最右邊的空字串 "" 被認為出現在索引值 this.length() 處。

返回的索引是最大的值 k,使得this.startsWith(str, k) 為真。

宣告

以下是Java String lastIndexOf()方法的宣告

public int lastIndexOf(String str)

引數

str − 這是要搜尋的子字串。

返回值

如果字串引數在此物件的子字串中出現一次或多次,則返回最後一個此類子字串的第一個字元的索引。如果它沒有作為子字串出現,則返回 -1。

異常

Java - String lastIndexOf(String str, int fromIndex) 方法

描述

Java String lastIndexOf(String str, int fromIndex) 方法返回此字串中指定子字串最後一次出現的索引,從指定的索引開始向後搜尋。返回的整數是最大的值 k,使得 -

k >= Math.min(fromIndex, this.length()) && this.startsWith(str, k),如果不存在這樣的 k 值,則返回 -1。

宣告

以下是Java String lastIndexOf()方法的宣告

public int lastIndexOf(String str, int fromIndex)

引數

  • str − 這是要搜尋的子字串。

  • fromIndex − 這是開始搜尋的索引。

返回值

此方法返回此字串中指定子字串最後一次出現的索引。

異常

從字串中獲取字元的最後一個索引示例

以下示例演示了 Java String lastIndexOf() 方法的用法。我們建立了一個字串字面量,其值為"This is tutorialspoint"。然後使用lastIndexOf()方法,我們檢索e的索引。

package com.tutorialspoint;

public class StringDemo {

   public static void main(String[] args) {

      String str = "This is tutorialspoint";
       
      // returns the index of last occurrence of character i
      System.out.println("last index of letter 'i' =  " 
         + str.lastIndexOf('i')); 
      
      // returns -1 as character e is not in the string
      System.out.println("last index of letter 'e' =  " 
         + str.lastIndexOf('e'));
   }
}

讓我們編譯並執行上述程式,這將產生以下結果 -

last index of letter 'i' = 19
last index of letter 'e' = -1

從字串中獲取字元的最後一個索引並帶索引示例

以下示例演示了 Java String lastIndexOf() 方法的用法。我們建立了一個字串字面量,其值為"This is tutorialspoint"。然後使用lastIndexOf()方法,我們檢索e的索引。

package com.tutorialspoint;

public class StringDemo {

   public static void main(String[] args) {

      String str = "This is tutorialspoint";
   
      /* returns positive value(last occurrence of character t) as character
         is located, which searches character t backward till index 14 */
      System.out.println("last index of letter 't' =  "
         + str.lastIndexOf('t', 14)); 
      
      /* returns -1 as character is not located under the give index,
         which searches character s backward till index 2 */
      System.out.println("last index of letter 's' =  "
         + str.lastIndexOf('s', 2)); 
      
      // returns -1 as character e is not in the string
      System.out.println("last index of letter 'e' =  "
         + str.lastIndexOf('e', 5));
   }
}

讓我們編譯並執行上述程式,這將產生以下結果 -

last index of letter 't' = 10
last index of letter 's' = -1
last index of letter 'e' = -1

從字串中獲取字串的最後一個索引示例

以下示例演示了 Java String lastIndexOf() 方法的用法。我們建立了一個字串字面量,其值為"Collections of tutorials at tutorials point"。然後使用lastIndexOf()方法,我們檢索tutorials的索引。

package com.tutorialspoint;

public class StringDemo {

   public static void main(String[] args) {

      String str1 = "Collections of tutorials at tutorials point";
     
      // returns index of first character of the last substring "tutorials" 
      System.out.println("index =  " + str1.lastIndexOf("tutorials")); 
      
      // returns -1 as substring "admin" is not located
      System.out.println("index =  " + str1.lastIndexOf("admin"));    
   }
}

讓我們編譯並執行上述程式,這將產生以下結果 -

index = 28
index = -1

從字串中獲取字串的最後一個索引並帶索引示例

以下示例演示了 Java String lastIndexOf() 方法的用法。我們建立了一個字串字面量,其值為"Collections of tutorials at tutorials point"。然後使用lastIndexOf()方法,我們檢索tutorials的索引。

package com.tutorialspoint;

public class StringDemo {

   public static void main(String[] args) {

      String str1 = "Collections of tutorials at tutorials point";
     
      /* search starts from index 17 and if located it returns 
         the index of the first character of the last substring "tutorials" */
      System.out.println("index = " + str1.lastIndexOf("tutorials", 17)); 

      /* search starts from index 9 and returns -1 as substring "admin"
         is not located */
      System.out.println("index = " + str1.lastIndexOf("admin", 9));     
   }
}

讓我們編譯並執行上述程式,這將產生以下結果 -

index = 15
index = -1
java_lang_string.htm
廣告