Wednesday, September 18

String Class & String manipulation in Java Part-1( Simple Examples for Character array values in String,Length Method,Concatenating Method,charAt(),compareTo(),compareToIgnoreCase(),contentEquals() Methods )

Hi All,

Here I wrote the program for String Class & String manipulation in java using simple examples :)

/*

character array values in String

Length Method 

Concatenating Method 

charAt() Method 

compareTo() Method 

compareToIgnoreCase() Method 

contentEquals() Method 

*/

import java.lang.String.*;

public class stringOperations {
    public static void main(String args[]){
        /* Using character array values in String method*/
       
        char[] var1={'a','b','c','d'};
        String val=new String(var1);
        System.out.println("character array values  :" + val);
       
        /* Length Method
         *
         * Note: It used to finding Length of the string values
         */

       
        String statement="Welcome to the string method";
        System.out.println("Length of the statement string  is "+statement.length());
       
        /*  Concatenating Method
         * 
         * Note: This method appends one String to the end of another String.
         */

        String fname="SKi";
        String lname="TECH";
        String username=fname.concat(lname);
        System.out.println("Concatenation of the Strings "+username);
       
        /*  charAt() Method
         *
         * Note: This method returns the character located at the String's specified index. The string indexes start from zero.
         */

       
        String statement1="Welcome to the string method";
        char res=statement1.charAt(11);
        System.out.println("The specified index of String is "+ res);
       
        /*  compareTo() Method
         *
         *  There are two variants of this method. First method compares this String to another Object and second method compares two strings lexicographically.
         *  Note: 
         *  Less than zero (<0)         The invoking string is less than string.
            Greater than zero (>0)    The invoking string is greater than string.
            Zero (0)                    The two strings are equal. 

         */

        String small_name="skitech";
        String uppername="SKITECH";
        String name="SKITECH";
        int result=small_name.compareTo(uppername);
        System.out.println("Result of compareTo method "+result);
        int result1=uppername.compareTo(name);
        System.out.println("Reault1 of compareTo method "+result1);
       
        /* compareToIgnoreCase() Method
         *
         * Note: This method compares two strings lexicographically, ignoring case differences.
         */

        int result2=small_name.compareToIgnoreCase(uppername);
        System.out.println("Result2 of compareToIgnoreCase method (ignoring case differences) "+result2);
        int result3=uppername.compareToIgnoreCase(name);
        System.out.println("Result3 of compareToIgnoreCase mathod (ignoring case differences) "+result3);
       
        /* contentEquals() Method
         *
         * Note: This method returns true if and only if this String represents the same sequence of characters as the specified in StringBuffer(Equally same), otherwise false.
         */

        String findstring="Skitech";
        StringBuffer statement4=new StringBuffer("Skitech Corporation is an Indian software corporation headquartered in Chennai, "
                + " that develops, manufactures, licenses, and supports a wide range of products and services related to computing. "
                + " The company was founded by Balamurugan and Sri on April 1, 2010. "
                + " Skitech is the world's largest software maker ");
        StringBuffer statement5=new StringBuffer("Skitech");
        Boolean content1=findstring.contentEquals(statement4);
        System.out.println("Content1 contentEquals() Method is  "+content1);
        boolean content2=findstring.contentEquals(statement5);
        System.out.println("Content2  contentEquals() Method is "+content2);
    }
   
}


output:

character array values  :abcd
Length of the statement string  is 28
Concatenation of the Strings SKiTECH
The specified index of String is t
Result of compareTo method 32
Reault1 of compareTo method 0
Result2 of compareToIgnoreCase method (ignoring case differences) 0
Result3 of compareToIgnoreCase mathod (ignoring case differences) 0
Content1 contentEquals() Method is  false
Content2  contentEquals() Method is true

No comments:

Post a Comment

Backup files to google drive using PHP coding

 Dear All, To backup files to Google Drive using PHP coding, you can use the Google Drive API and the Google Client Library for PHP. Here...