Java StringBuilder indexOf() 方法



Java StringBuilder indexOf() 方法用於檢索字串或 StringBuilder 中指定字元第一次出現的索引,該方法區分大小寫,這意味著字串“A”“a”是兩個不同的值。

indexOf() 方法接受一個引數作為字串,該引數儲存子字串的值。它返回一個整數值,如果當前字串中不存在給定值,則返回-1。如果給定的字串值為null,則該方法會丟擲異常。

indexOf() 方法有兩個多型變體,具有不同的引數,例如 - 字串和 fromIndex(以下是兩種多型變體的語法)。

語法

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

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

引數

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

返回值

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

如果將值傳遞給fromIndex引數,則此方法將返回從指定索引開始的指定子字串的第一次出現的索引。

示例:獲取子字串的索引

如果給定的字串值不為 null並且存在於當前字串中,則此方法返回其位置。

在這個程式中,我們使用值“Welcome to TutorialsPoint”建立一個StringBuilder物件。然後,使用indexOf()方法,我們嘗試檢索子字串“to”的索引。

package com.tutorialspoint.StringBuilder;
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
        String str = "to";
        System.out.println("The sustring is: " + str);
        //using the indexOf() method
        System.out.println("The position of " + str + " is: " + sb.indexOf(str));
    }
}

輸出

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

The given string is: Welcome to TutorialsPoint
The sustring is: to
The position of to is: 8

示例:獲取 null 的索引時遇到 NullPointerException

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

在下面的示例中,我們用 null 值例項化一個StringBuilder。使用indexOf()方法,我們嘗試檢索 StringBuilder 中指定字元第一次出現的位置。由於給定的字串值為null,因此該方法會丟擲異常。

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

輸出

以下是上述程式的輸出:

java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null
	at java.base/java.lang.AbstractStringBuilder.(AbstractStringBuilder.java:105)
	at java.base/java.lang.StringBuilder.(StringBuilder.java:131)
	at com.tutorialspoint.StringBuilder.Index.main(Index.java:6)
Exception: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null

示例:獲取不存在的子字串的索引

如果在 StringBuilder 中找不到給定的字串值,則 indexOf() 方法返回-1

在下面的程式中,我們使用值“TutorialsPoint”建立一個StringBuilder物件。然後,使用indexOf()方法,我們嘗試檢索給定 StringBuilder 中“tutorix”的位置。

package com.tutorialspoint.StringBuilder;
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
      String str = "tutorix";
      System.out.println("The sustring is: " + str);
      //using the indexOf() method
      System.out.println("The position of " + str + " is: " + sb.indexOf(str));
   }
}

輸出

上述程式產生以下輸出:

The given string is: TutorialsPoint
The sustring is: tutorix
The position of tutorix is: -1

示例:獲取子字串的索引

如果給定的字串值不為 null並且 fromIndex 值為正數,則 indexOf() 方法返回子字串的位置。

在這個示例中,我們用值“Java Programming Language”例項化StringBuilder。然後使用indexOf()方法,我們嘗試從索引10檢索“language”子字串的位置。

package com.tutorialspoint.StringBuilder;
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 = "Language";
      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.indexOf(str, fromIndex));
   }
}

輸出

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

The given string is: Java Programming Language
The sustring is: Language
The fromIndex value is: 10
The position of Language is: 17
java_lang_stringbuilder.htm
廣告