📜  Java字符串 indexOf()

📅  最后修改于: 2022-05-13 01:54:35.710000             🧑  作者: Mango

Java字符串 indexOf()

indexOf() 方法有四种变体。本文描述了所有这些,如下所示:
1.int indexOf() :此方法返回此字符串中指定字符第一次出现的索引,如果该字符未出现,则返回-1。

Syntax:
int indexOf(char ch )
Parameters:
ch : a character.
Java
// 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'));
    }
}


Java
// 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));
    }
}


Java
// 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));
    }
}


Java
// 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 after 14th position
        // prints 19
        System.out.print("Found geeks(after 14th index) starting at position : ");
        System.out.print(Str.indexOf(subst, 14));
    }
}


JAVA
class Vowels
{
        // function to check if the passed 
        // character is a vowel
    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


输出
Found g first at position : 11

2. int indexOf(char ch, int strt) :此方法返回此字符串中第一次出现指定字符的索引,从指定索引开始搜索,如果没有出现该字符,则返回-1。

Syntax:
int indexOf(char ch, int strt)
Parameters:
ch :a character.
strt : the index to start the search from.

Java

// 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.int indexOf(String str) :该方法返回该字符串中指定子字符串第一次出现的索引。如果它不作为子字符串出现,则返回 -1。

Syntax:
int indexOf(String str)
Parameters:
str : a string.

Java

// 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. int indexOf(String str, int strt) :该方法返回该字符串中指定子字符串第一次出现的索引,指定索引开始。如果没有发生,则返回 -1。

Syntax:
int indexOf(String str, int strt)
Parameters:
strt: the index to start the search from.
str : a string.
 

Java

// 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 after 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

一些相关应用:

  • 找出给定字符(可能是大写或小写)是元音还是辅音。
    下面给出实现:

Java

class Vowels
{
        // function to check if the passed 
        // character is a vowel
    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