Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Monday, 4 June 2012

concat Method of String


public class MyClass {
public static void main(String[] args) {
    String str1 = "harry";
    String str2 = "jerry";
    System.out.println(str1.concat(str2));
}   
}
 Output:
harryjerry

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

startsWith Method of String


public class MyClass {
    public static void main(String[] args) {
        String [] words = {"tom","jerry","tomato","potato"};
       
        for(String w:words){
            if(w.startsWith("to")){
                System.out.println(w+" starts with to");
            }
        }
    }
}


Output:
tom starts with to
tomato starts with to