Sunday, February 24, 2013

Various String Methods





public class variousStringMethods {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
        String sentence,
               s1,
               s2,
               s3;
        int index;
       
        sentence = "Celebrate the season of gift-giving";
       
        System.out.println("sentence = \"" + sentence + "\"");
       
        System.out.println("the length of sentence = "
                + sentence.length());
       
        System.out.println(" sentence = \"" + sentence + "\"");
       
        System.out.println("the character at index 16 in "
                + "sentence = "
                + sentence.charAt(17));
       
        System.out.println("the character at index 16 in "
                + "sentence = "
                + sentence.charAt(17));
       
        System.out.println("the index of first s in  "
                + "sentence = "
                + sentence.indexOf('s'));
       
        System.out.println("the index of for in "
                + "sentence = "
                + sentence.indexOf("for"));
       
        System.out.println("sentence in uppercase = \n"
                + "        \""
                + sentence.toUpperCase() + "\"");
       
        index = sentence.indexOf(" season ");
       
        s1 = sentence.substring(index, index + 15);
       
       
        System.out.println("s1  = \""
                + s1 +"\"");
       
        s2 = "Super" + s1;
        System.out.println("s2  = \""
                + s2 + "\"");
       
        s3 = sentence.replace('s', 'S');
        System.out.println("s3  = \""
                + s3 + "\"");
       
       
       

    }

}

No comments:

Post a Comment