strings concepts in java with examples


import java.text.DecimalFormat;

public class StringExample {

    public static void main(String[] args) {
// Create Strings 
        String st1="Java and Selenium on ";
        String st2= new String ("I Love Selenium and Java");
        char arr[] = {'w','w','w','.','s','e','l','e','n','i','u','m','l','e','a','r','n','.','c','o','m'};
        String st3=new String(arr);

// Display all the 3 Strings

// Find length of k1 string
        System.out.println("Length of k1="+st1.length());

// Adding two k1+k2 strings
        System.out.println("k1 and k2 added="+st1.concat(st2));

//Concatenate k1 string with k3 
        System.out.println(st1+"Visit--> "+st3);

// Test if string k2 start with I
        boolean b=st2.startsWith("I");
        if(b) System.out.println("k2 string starts with \'I\'");
        else System.out.println("k2 string does not start with \'I\'");

//Extract substring from k2, starting from 8th char to 23rd char
        String p =st2.substring(7,24);

//Extract substring from k3, starting from 3rd char to 12th char
        String q =st3.substring(3,12);

//convert k3 into upper case to  lower case and upper case to lower case.
        System.out.println("Lower case = "+st3.toLowerCase());
        System.out.println("Upper case = "+st3.toUpperCase());

// Check if k1 string equal to k2 string

        String st4="java";
        String st5="JAVA";
        if (st4==st5)     // == is Case Sensitive
            System.out.println("k1 & k2 strings  are same");
        else System.out.println("k1 & k2 strings  are NOT same");

        if(st4.equalsIgnoreCase(st5)) // equalsIgnoreCase is "Insensitive comparison"
            System.out.println("k1 & k2 strings are Same");
        else System.out.println("k1 & k2 strings  are NOT same");

        if(st4.equals(st5)) // equals is case sensitive
            System.out.println("k1 & k2 strings are Same");
        else System.out.println("k1 & k2 strings  are NOT same");

// Convert original to reverse or reverse to origina
        String original = "moc.nraelmuineles.www";
        System.out.println("Original String : " + original);
        original = new StringBuffer(original).reverse().toString();
        System.out.println("Reversed String : " + original);

// Copying a string into an array
        String k6= "SeleniumLearn is now SeleniumTool";
        char array[] = new char[30];
        k6.getChars(9, 25, array, 0);

// Splitting a string
        String st7 = "I Love Opensource Tools"; // String k7 which is to broken
        String s[]; // declare a string type array s to store pieces
        s=st7.split(" ");
        for (int i=0;i<s.length;i++)

// Remove empty spaces before and after in a string
        String st8 = "     www.       ";  
        System.out.println(st8+"");//without trim()  
        System.out.println(st8.trim()+"");//with trim()  "

//Convert using Integer.toString(int)
        int integer = 91 ;
        String test1 = Integer.toString(integer);
        System.out.println("String converting an int to String is = "+ test1);

//Convert using String.valueOf(int)    
        int number = 120;
        String test2 = String.valueOf(number);
        //String test2 = String.valueOf(-120);  // Shortcut
        System.out.println("String converting an String to value is = "+ test2);

//Convert using new Integer(int).toString()

       int id = 109;
       Integer obj = new Integer(id);      
       String test3 = obj.toString();*/

       int id = 109;
       String test3 = new Integer(id).toString();*/
        String test3 = new Integer(109).toString();

        System.out.println("New integer Instance to String is = "+ test3);

//Convert using String.format()
        int test4 = 9000;
        String numberasString = String.format ("%d", test4);
        System.out.println("Convert using String.format is = "+ test4);

//Convert using DecimalFormat
        int id = 9000109;
        DecimalFormat dF = new DecimalFormat("#,##");
        String numAsSt = dF.format(id);
        System.out.println("Convert using DecimalFormat is: "+ numAsSt);
//    Convert using StringBuffer
        /*int num = -420;
        StringBuffer sb = new StringBuffer();
        String numberisString = sb.toString();*/
        String num = new StringBuffer().append(-420).toString();
        System.out.println("Convert using StringBuffer is :" + num);
//Convert using  StringBuilder
        /*int numb = 420;
        StringBuilder sb = new StringBuilder();
        String numberASString = sb.toString();*/
        String numb = new StringBuilder().append(420).toString();
        System.out.println("Convert using  StringBuilder is :" + numb);

Output :-
Java and Selenium on 
I Love Selenium and Java
Length of k1=39
k1 and k2 added=Java and Selenium on I Love Selenium and Java
Java and Selenium on Visit-->
k2 string starts with 'I'
Selenium and Java.selenium
Lower case =
k1 & k2 strings  are NOT same
k1 & k2 strings are Same
k1 & k2 strings  are NOT same
Original String : moc.nraelmuineles.www
Reversed String :
earn is now Sele


String converting an int to String is = 91
String converting an String to value is = 120
New integer Instance to String is = 109
Convert using String.format is = 9000
Convert using DecimalFormat is: 9,00,01,09
Convert using StringBuffer is :-420
Convert using  StringBuilder is :420