Which of the following method call gives the position of the Firstoccurrence of x in the string S1?

There are four variants of indexOf() method. This article depicts about all of them, as follows: 
1.int indexOf() : This method returns the index within this string of the first occurrence of the specified character or -1, if the character does not occur.
 

Syntax: int indexOf(char ch ) Parameters: ch : a character.

Java

public class Index1 {

public static void main(String args[])

    {

        String gfg = new String("Welcome to geeksforgeeks");

        System.out.print("Found g first at position : ");

        System.out.println(gfg.indexOf('g'));

    }

}

Output

Found g first at position : 11

2. int indexOf(char ch, int strt ) : This method returns the index within this string of the first occurrence of the specified character, starting the search at the specified index or -1, if the character does not occur.
 

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

Java

public class Index2 {

public static void main(String args[])

    {

        String gfg = new String("Welcome to geeksforgeeks");

        System.out.print("Found g after 13th index at position : ");

        System.out.println(gfg.indexOf('g', 13));

    }

}

Output

Found g after 13th index at position : 19

3.int indexOf(String str) : This method returns the index within this string of the first occurrence of the specified substring. If it does not occur as a substring, -1 is returned.
 

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

Java

public class Index3 {

public static void main(String args[])

    {

        String Str = new String("Welcome to geeksforgeeks");

        String subst = new String("geeks");

        System.out.print("Found geeks starting at position : ");

        System.out.print(Str.indexOf(subst));

    }

}

Output

Found geeks starting at position : 11

4. int indexOf(String str, int strt) : This method returns the index within this string of the first occurrence of the specified substring, starting at the specified index. If it does not occur, -1 is returned. 
 

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

Java

public class Index4 {

public static void main(String args[])

    {

        String Str = new String("Welcome to geeksforgeeks");

        String subst = new String("geeks");

        System.out.print("Found geeks(after 14th index) starting at position : ");

        System.out.print(Str.indexOf(subst, 14));

    }

}

Output

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

Some related applications:
 

  • Finding out if a given character (maybe anything upper or lower case) is a vowel or consonant. 
    Implementation is given below: 
     

JAVA

class Vowels

{

    public static boolean vowel(char c)

    {

        return "aeiouAEIOU".indexOf(c)>=0;

    }

    public static void main(String[] args)

    {

        boolean isVowel = vowel('a');

                if(isVowel)

            System.out.println("Vowel");

        else

            System.out.println("Consonant");

    }

}

This article is contributed by Astha Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. 
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


Which method call gives the position of the first occurrence of x in the string s1?

The Java String class indexOf() method returns the position of the first occurrence of the specified character or string in a specified string.

Which of the following method call gives the position of X that occurs after?

The charAt() method returns the character at the specified index in a string. The index of the first character is 0, the second character is 1, and so on.

What is the use of charAt () method Mcq?

Explanation: charAt() is a method of class String which gives the character specified by the index. obj.

Which of the following methods will create string in Java Mcq?

The intern() and toString() methods are of String class.