當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java String indexOf()用法及代碼示例


indexOf()方法有四個變體。本文介紹了所有這些元素,如下所示:1.String indexOf():此方法返回指定字符首次出現在字符串中的索引,如果未出現則返回-1。

用法:
public int indexOf(int ch )
參數:
ch: a character.
// Java code to demonstrate the working 
// of String indexOf() 
public class Index1 { 
public static void main(String args[]) 
    { 
  
        // Initialising String 
        String gfg = new String("Welcome to geeksforgeeks"); 
  
        System.out.print("Found g first at position:"); 
  
        // Initial index of 'g' will print 
        // prints 11 
        System.out.println(gfg.indexOf('g')); 
    } 
}

輸出:

Found g first at position:11

2.字符串indexOf(char ch,int strt):此方法返回指定字符首次出現在該字符串中的索引,如果沒有出現該字符,則從指定索引或-1開始搜索。


用法:
public int indexOf(int ch, int strt)
參數:
ch :a character.
strt:the index to start the search from.

// Java code to demonstrate the working 
// of String indexOf(char ch, int strt) 
public class Index2 { 
public static void main(String args[]) 
    { 
  
        // Initialising String 
        String gfg = new String("Welcome to geeksforgeeks"); 
  
        System.out.print("Found g after 13th index at position:"); 
  
        // 2nd index of 'g' will print 
        // prints 19 
        System.out.println(gfg.indexOf('g', 13)); 
    } 
}

輸出:

Found g after 13th index at position:19

3.String indexOf(String str):此方法返回指定子字符串首次出現在此字符串中的索引。如果它不是作為子字符串出現,則返回-1。

用法:
int indexOf(String str)
參數:
str:a string.
// Java code to demonstrate the working 
// of String indexOf(String str) 
public class Index3 { 
public static void main(String args[]) 
    { 
  
        // Initialising string 
        String Str = new String("Welcome to geeksforgeeks"); 
  
        // Initialising search string 
        String subst = new String("geeks"); 
  
        // print the index of initial character 
        // of Substring 
        // prints 11 
        System.out.print("Found geeks starting at position:"); 
        System.out.print(Str.indexOf(subst)); 
    } 
}

輸出:

Found geeks starting at position:11

4.String indexOf(String str,int strt):此方法從指定的索引開始,返回指定子字符串首次出現在該字符串中的索引。如果沒有發生,則返回-1。

用法:
int indexOf(String str, int strt)
參數:
strt:the index to start the search from.
str:a string.
// Java code to demonstrate the working 
// of String indexOf(String str, int strt) 
public class Index4 { 
public static void main(String args[]) 
    { 
  
        // Initialising string 
        String Str = new String("Welcome to geeksforgeeks"); 
  
        // Initialising search string 
        String subst = new String("geeks"); 
  
        // print the index of initial character 
        // of Substring aftr 14th position 
        // prints 19 
        System.out.print("Found geeks(after 14th index) starting at position:"); 
        System.out.print(Str.indexOf(subst, 14)); 
    } 
}

輸出:

Found geeks(after 14th index) starting at position:19

一些相關的應用程序:

  • 找出給定的字符(可能是大寫還是小寫)是元音還是輔音。

    具體實現如下:

    class Vowels 
    { 
            // function to check if the passed   
            // character is a vovel 
        public static boolean vowel(char c) 
        { 
            return "aeiouAEIOU".indexOf(c)>=0; 
        } 
      
            // Driver program 
        public static void main(String[] args)  
        { 
            boolean isVowel = vowel('a'); 
              
                    // Printing the output 
                    if(isVowel) 
                System.out.println("Vowel"); 
            else
                System.out.println("Consonant"); 
        } 
    } 
      
    // This code is contributed by debjitdbb

    輸出:

    Vowel
    


相關用法


注:本文由純淨天空篩選整理自 Java String indexOf()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。