Java - String indexOf() 方法



描述

Java String indexOf(int ch) 方法返回指定字元在此字串中第一次出現的索引。

如果字元值ch出現在此 String 物件表示的字元序列中,則返回第一次出現的索引(Unicode 程式碼單元)。

宣告

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

public int indexOf(int ch)

引數

ch − 這是一個字元(Unicode 程式碼點)。

返回值

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

異常

String indexOf(int ch, int fromIndex) 方法

描述

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

如果字元值ch出現在此 String 物件表示的字元序列中,且索引不小於fromIndex,則返回第一次出現的索引。

宣告

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

public int indexOf(int ch, int fromIndex)

引數

  • ch − 這是一個字元(Unicode 程式碼點)。

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

返回值

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

異常

String indexOf(String str) 方法

描述

Java String indexOf(String str) 方法返回指定子字串在此字串中第一次出現的索引。返回的整數是最小的值 k,使得:this.startsWith(str, k) 為真。

宣告

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

public int indexOf(String str)

引數

str − 這是一個字串的值。

返回值

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

異常

String indexOf(String str, int fromIndex) 方法

描述

Java String indexOf(String str, int fromIndex) 方法返回指定子字串在此字串中第一次出現的索引,從指定索引開始。返回的整數是最小的值 k,對於該值:

k > = Math.min(fromIndex, this.length()) && this.startsWith(str, k)

如果不存在此類 k 值,則返回 -1。

宣告

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

public int indexOf(String str, int fromIndex)

引數

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

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

返回值

此方法返回指定子字串在此字串中第一次出現的索引,從指定索引開始。

異常

從字串中獲取字元索引示例

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

package com.tutorialspoint;

public class StringDemo {

   public static void main(String[] args) {

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

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

index of letter 's' = 3
index of letter 'e' = -1

從字串中獲取字串索引示例

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

package com.tutorialspoint;

public class StringDemo {

   public static void main(String[] args) {
 
      String str = "This is tutorialspoint";

      // returns positive value as character is located
      System.out.println("index of letter 't' =  "
         + str.indexOf('t', 14)); 
      
      // returns positive value as character is located
      System.out.println("index of letter 's' =  "
         + str.indexOf('s', 10)); 
      
      // returns -1 as character is not in the string
      System.out.println("index of letter 'e' =  "
         + str.indexOf('e', 5));
   }
}

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

index of letter 't' = 21
index of letter 's' = 16
index of letter 'e' = -1

從字串中獲取字元索引示例

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

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 substring "tutorials" 
      System.out.println("index =  " + str1.indexOf("tutorials")); 
      
      // returns -1 as substring "admin" is not located
      System.out.println("index =  " + str1.indexOf("admin"));    
   }
}

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

index = 15
index = -1

使用 FromIndex 從字串中獲取字串索引示例

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

package com.tutorialspoint;

public class StringDemo {

   public static void main(String[] args) {

      String str1 = "Collections of tutorials at tutorials point";
     
      /*  search starts from index 10 and if located it returns 
         the index of the first character of the substring "tutorials" */
      System.out.println("index = " + str1.indexOf("tutorials", 10)); 
      
      /* search starts from index 9 and returns -1 as substring "admin"
         is not located */
      System.out.println("index = " + str1.indexOf("admin", 9));     
   }
}

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

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

© . All rights reserved.