Saturday, December 7, 2013

Java Programs to demonstrate use of String

1)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class UseOfString {

    /**
     * @param args
     */
    public static void main(String[] args) throws IOException{
        // TODO Auto-generated method stub
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.print(" Enter the String ");
        String content=reader.readLine();
        for(int index=content.length()-1;index>=0;index--)
            System.out.print(content.charAt(index));
       
        System.out.println();
        System.out.println(" Enter thye String to be searched ");
        String subStr=reader.readLine();
        int location=content.indexOf(subStr);
        System.out.println(" The index is : "+location);
       
        location=content.indexOf(subStr, 5);
        System.out.println(" The index is : "+location);
             
        System.out.println(" UpperCase ::: "+content.toUpperCase());
        System.out.println(" LowerCase ::: "+content.toLowerCase());
       
       
    }

}

2)


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class UseOfString_1 {

    /**
     * @param args
     */
    public static void main(String[] args) throws IOException{
        // TODO Auto-generated method stub
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String source="";
        System.out.print(" Enter the 1st String ");
        source=reader.readLine();
        String subStr=source.substring(5);
        System.out.println(subStr);
       
        subStr=source.substring(6,8);
        System.out.println(subStr);
       
        System.out.println(" Enter the second string ");
        String destination=reader.readLine();
       
        System.out.println(source.equals(destination));
        System.out.println(source.equalsIgnoreCase(destination));
       
       
       
       
    }
}

3)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class UseString_2 {
   
    public static void main(String[] args) throws IOException{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println(" Enter the 1st String ");
        String first=reader.readLine();
        System.out.println(" Enter the 2nd String ");
        String second=reader.readLine();
        System.out.println(first.compareTo(second));
        System.out.println(first.compareToIgnoreCase(second));
       
        System.out.println(12+25+first+" "+second);
        System.out.println(first+" "+second+12+25);  // Once a string is encountered rest all are converted to string
       
        String cnvrt=12+" ";
        String srt="first-second-third";
        String arr[]=srt.split("-");
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr[2]);
    }

}

No comments:

Post a Comment