当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。