Demonstrating Few String Manipulation Functions in Java

In this program, I am going to show the use of few string manipulation functions in java.

import java.io.*;
public class StringManipulation 
{
    public static void main(String[] args)throws IOException
    {
        BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter First String");
        String s1=obj.readLine();
        System.out.println("Enter Second String");
        String s2=obj.readLine();
        System.out.println("First String is :"+s1);
        System.out.println("Second String is :"+s2);
        System.out.println("String 1 in lower case :"+s1.toLowerCase());
        System.out.println("String 2 in upper case :"+s2.toUpperCase());
        System.out.println("Length of first string :"+s1.length());
        System.out.println("Length of Second string :"+s2.length());
        System.out.println("String after concat :"+s1.concat(s2));
        System.out.println("First String after trimming "+s1.trim());
        System.out.println("2nd character in string 1 :"+s1.charAt(2));
        System.out.println("2nd character in string 2 :"+s2.charAt(2));
        System.out.println("Is both strings are equal :"+s1.equals(s2));
        System.out.println("IS both Strings are equal(Ignore case) :"+s1.equalsIgnoreCase(s2));
        System.out.println("Index of 'a' in second string :"+s2.indexOf('a'));
    }
}

/* Output
Enter First String
Ankur
Enter Second String
Mhatre
First String is :Ankur
Second String is :Mhatre
String 1 in lower case :ankur
String 2 in upper case :MHATRE
Length of first string :5
Length of Second string :6
String after concat :AnkurMhatre
First String after trimming Ankur
2nd character in string 1 :k
2nd character in string 2 :a
Is both strings are equal :false
IS both Strings are equal(Ignore case) :false
Index of 'a' in second string :2
*/

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.