Java StringBuilder lastIndexOf() 方法



Java StringBuilder lastIndexOf() 方法用於在 StringBuilder 物件中檢索給定字串最右邊出現的索引。索引指的是字串中字元的位置。

lastIndexOf() 方法接受一個字串引數,該引數包含子字串的值。如果給定的字串為 null,則會丟擲異常。該方法返回一個整數值,如果未找到子字串,則返回 -1

lastIndexOf() 方法有兩個多型變體,具有不同的引數,例如 - StringfromIndex(以下是兩種多型變體的語法)。

語法

以下是 Java StringBuilder lastIndexOf() 方法的語法:

public int lastIndexOf(String str)
public int lastIndexOf(String str, int fromIndex)

引數

  • str − 這是要搜尋的子字串。
  • fromIndex − 這是開始搜尋的索引。

返回值

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

如果將fromIndex 值作為引數與字串一起傳遞,則此方法將返回在此序列中指定子字串最後一次出現的索引。

示例:獲取子字串的最後一個索引

如果給定的字串值不為 null,則 lastIndexOf() 方法返回子字串的索引值。

在下面的程式中,我們使用值“Welcome to TutorialsPoint” 例項化StringBuilder。使用lastIndexOf() 方法,我們試圖檢索“to” 子字串的索引。

public class Index {
   public static void main(String[] args) {
      // instantiating the StringBuilder
      StringBuilder sb = new StringBuilder("Welcome to TutorialsPoint");
      System.out.println("The given string is: " + sb);
      // initialize the substring and fromIndex value
      String str = "to";
      System.out.println("The substring is: " + str);
      // using the indexOf() method
      System.out.println("The index value of '" + str + "' is: " + sb.lastIndexOf(str));
   }
} 

輸出

執行上述程式後,將產生以下結果:

The given string is: Welcome to TutorialsPoint
The substring is: to
The index value of 'to' is: 13

示例:獲取 null 子字串的最後一個索引時遇到 NullPointerException

如果給定的字串值為null,則 lastIndexOf() 方法將丟擲NullPointerException

在下面的程式中,我們使用值“HelloWorld”建立一個StringBuilder 物件。使用lastIndexOf() 方法,我們試圖檢索 null 值的索引。

public class Index {
   public static void main(String[] args) {
      try {
         //instantiating the StringBuilder
         StringBuilder sb = new StringBuilder("HelloWorld");
         System.out.println("The given string is: " + sb);
         //initialize the substring and fromIndex value
         String str = null;
         int fromIndex = 5;
         System.out.println("The sustring is: " + str);
         System.out.println("The fromIndex value is: " + fromIndex);
         //using the indexOf() method
         System.out.println("The position of " + str + " is: " + sb.lastIndexOf(str, fromIndex));
      } catch(NullPointerException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

以下是上述程式的輸出:

The given string is: HelloWorld
The sustring is: null
The fromIndex value is: 5
java.lang.NullPointerException: Cannot read field "value" because "tgtStr" is null
   at java.base/java.lang.String.lastIndexOf(String.java:2628)
   at java.base/java.lang.AbstractStringBuilder.lastIndexOf(AbstractStringBuilder.java:1521)
   at java.base/java.lang.StringBuilder.lastIndexOf(StringBuilder.java:698)
   at Index.main(Index.java:15)
Exception: java.lang.NullPointerException: Cannot read field "value" because "tgtStr" is null

示例:獲取不存在的子字串的最後一個索引

如果給定的字串值不為 null,但fromIndex 值為負數,則此方法返回-1

在下面的示例中,我們使用值“Java Programming Language”例項化StringBuilder。然後,使用lastIndexOf() 方法,我們試圖從索引-1檢索“Programming” 子字串的索引。

public class Index {
   public static void main(String[] args) {
      //instantiating the StringBuilder
      StringBuilder sb = new StringBuilder("Java Programming Language");
      System.out.println("The given string is: " + sb);
      //initialize the substring and fromIndex value
      String str = "Programming";
      int fromIndex = -1;
      System.out.println("The sustring is: " + str);
      System.out.println("The fromIndex value is: " + fromIndex);
      //using the indexOf() method
      System.out.println("The position of " + str + " is: " + sb.lastIndexOf(str, fromIndex));
   }
}

輸出

上述程式產生以下結果:

The given string is: Java Programming Language
The sustring is: Programming
The fromIndex value is: -1
The position of Programming is: -1

示例:獲取子字串的最後一個索引

如果 fromIndex 值為正數並且大於子字串索引值,則 lastIndexOf() 方法將返回子字串的索引值。

在下面的程式中,我們使用值“TutorialsPoint”建立一個StringBuilder。使用lastIndexof() 方法,我們試圖從索引 10 檢索“als” 子字串的索引。

public class Index {
   public static void main(String[] args) {
      //instantiating the StringBuilder
      StringBuilder sb = new StringBuilder("TutorialsPoint");
      System.out.println("The given string is: " + sb);
      //initialize the substring and fromIndex value
      String str = "als";
      int fromIndex = 10;
      System.out.println("The sustring is: " + str);
      System.out.println("The fromIndex value is: " + fromIndex);
      //using the indexOf() method
      System.out.println("The position of " + str + " is: " + sb.lastIndexOf(str, fromIndex));
   }
}

輸出

執行上述程式後,將產生以下輸出:

The given string is: TutorialsPoint
The sustring is: als
The fromIndex value is: 10
The position of als is: 6
java_lang_stringbuilder.htm
廣告