You are here: Home / Topics / index of() method example in Java

index of() method example in Java

Filed under: Java on 2023-08-23 18:54:04

// Program to explain the following String function :
// int indexOf(int ch) 
// int indexOf( String str )

class  MindexOf
{
public static void main(String args[ ])
{
 String s1 = "Java Programming Examples Java Programming Examples.";
   
 System.out.println(" Index of " + 'P' + " = " + s1.indexOf('P'));
 System.out.println(" Index of " + 'x' + " = " + s1.indexOf('x'));
 System.out.println(" Index of " + "Examples" + " = " + s1.indexOf("Examples"));
 System.out.println(" Index of " + "JAVA" + " = " + s1.indexOf("JAVA"));
}
}


Output:

Index of P = 5
Index of x = 18
Index of Examples = 17
Index of JAVA = -1

About Author:
J
Java     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.