Monday 4 June 2012

indexOf Method of String

public class MyClass {public static void main(String[] args) {
    String str = "harryjerryharryjerryharry";
    System.out.println(str.indexOf('e'));
}   
}

Output:
6


The method str.indexOf('e') will return the index of letter e of first occurrence in the string


Another example:
public class MyClass {
public static void main(String[] args) {
    String str = "harryjerryharryjerryharry";
    System.out.println(str.indexOf("yj"));
}  
}
Output:
4

No comments:

Post a Comment

Note: only a member of this blog may post a comment.