Wednesday, September 25

String Class & String manipulation in Java Part-2( Simple Examples for substring(),toCharArray(),toLowerCase(),toUpperCase(),toString(),integer to string in java, double to string in java,trim(),indexOf(),intern() Methods )

Hi All,

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

  /*

substring() Method

toCharArray() Method

toLowerCase() Method 

toUpperCase() Method 

toString() Method 

integer to string in java 

double to string in java  

trim() Method 

indexOf() Method 

intern() Method 

*/

import java.lang.String.*; 

public class stringOperations2 {
    public static void main(String args[]){
       /*String Manipuldation Part-2*/
       
        /* String substring() Method
         * 
         * Note: The substring begins with the character at the specified index and extends to the end of this string or up to endIndex - 1 if second argument is given.
         */
        String name="SKI Technology";
        String cName=name.substring(0,8);
        System.out.println("CName value in substring() Method :"+cName);
        String cName1=name.substring(4);
        System.out.println("CName1 value is substring() Method (No EndIndex it will take upto endIndex - 1 if second argument is given): "+cName1);
       
        /*  toCharArray() Method
         *
         * Note: This method converts this string to a new character array.
         */
        String val=new String("My Name is SkiTECH");
        char[] b=val.toCharArray();
        System.out.print("Result of the toCharArray() Method ");
        for(char temp: b){
       System.out.print(temp);
    }
       
        /*  String toLowerCase() Method
         *
         * Note: It returns the String, converted to lowercase.
         */
        String cname="SkiTECH";
        System.out.println("\nResult of the String toLowerCase() Method "+cname.toLowerCase());
       
        /* String toUpperCase() Method
         *
         * Note: It returns the String, converted to uppercase.
         */
        String cname1="skitech";
        System.out.println("Result of the String toUpperCase() Method "+cname1.toUpperCase());
       
        /*  toString() Method
         *
         * Note: This method returns the string itself.
         */
        String tname=new String("welcome");
        System.out.println("Result of the toString() Method "+tname.toString());
       
        /*  integer to string in java using toString() method */
        int n=10;
        String sval=Integer.toString(n);
        System.out.println("Integer to string using toString() Method "+sval);
       
        /* double to string in java using toString() method */
        double aDouble = 0.11;
        String sval1 = Double.toString(aDouble);
        System.out.println("Double to string using toString() Method "+sval1);
       
        /*  trim() Method
         *
         * Note: It returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.
         */
        String temp1=" welcome to the SKiTECH          ";
        System.out.println("Result of the trim() Method "+temp1.trim());
       
        /*  indexOf() Method
         *
         * Note:
           1. public int indexOf(int ch): Returns the index within this string of the first occurrence of the specified character or -1 if the character does not occur.
           2. public int indexOf(int ch, int fromIndex): Returns the index within this string of the first occurrence of the specified character, starting the search at
              the specified index or -1 if the character does not occur.
           3. int indexOf(String str): Returns the index within this string of the first occurrence of the specified substring. If it does not occur as a substring, -1 is returned.
           4. int indexOf(String str, int fromIndex): Returns the index within this string of the first occurrence of the specified substring,
              starting at the specified index. If it does not occur, -1 is returned.
         */
        String str1="Ski technology in technopark";
        String str2="tech";
        System.out.println("Result of indexOf(int ch) method "+str1.indexOf('t'));
        System.out.println("Result of indexOf(int ch, int fromIndex) method "+str1.indexOf('o',13));
        System.out.println("Result of indexOf(String str) method "+str1.indexOf(str2));
        System.out.println("Result of indexOf(String str, int fromIndex) method "+str1.indexOf(str2, 10));
       
        /*  intern() Method
         *
         * Note: This method Returns a canonical representation for the string object.
         */
        String str3="welcome To Skitech";
        System.out.println("Result of the intern() Method "+str3.intern());
    }
   
}

  OUTPUT:

CName value in substring() Method :SKI Tech
CName1 value is substring() Method (No EndIndex it will take upto endIndex - 1 if second argument is given): Technology
Result of the toCharArray() Method My Name is SkiTECH
Result of the String toLowerCase() Method skitech
Result of the String toUpperCase() Method SKITECH
Result of the toString() Method welcome
Integer to string using toString() Method 10
Double to string using toString() Method 0.11
Result of the trim() Method welcome to the SKiTECH
Result of indexOf(int ch) method 4
Result of indexOf(int ch, int fromIndex) method 23
Result of indexOf(String str) method 4
Result of indexOf(String str, int fromIndex) method 18
Result of the intern() Method welcome To Skitech

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...