Thursday, April 3, 2014

String – III



 String.valueOf(String) - Returns a new Integer object initialized to the value of the specified

Example:

            String x="12";

            int y=Integer.valueOf(x);

String.toCharArray() -Converts this string to a new character array.

Example:

            String Str=”This is a test”;

            char ch[]=Str.toCharArray();

String copyValueOf(char[] dataItem, int offset, int count) : This function create a new string object from the contents stored in a character array.

            dataItem - is the given character array.

            offset - Starting position in the character  array. The position should be
            grater than 0 and less than the array length.

            count - Indicate the numbers of character to be picked from the character
            array.

Example:

            char[] chArr = new char[] { 'A', 'B', 'C', 'D','E','F' };
            String str = String.copyValueOf(chArr, 1, 3); //Returns "BCD"

boolean regionMatches(int, String,int,int) : Checks if specified region of a string matches with the another given region of the String argument and return Boolean.

Example:
      
            String str = "I live in India";
            String str1 = "I love India";

            boolean flag = str.regionMatches(10, str1, 7, 5); //Return true

toString() - Widely used when an object needed to represent as a string.'toString()' funcction returns the string representation of an object.

Example:

            Integer Num = 5;

            System.out.println(Num.toString()); 
                            
                                      or

             System.out.println(Integer.toString(375)); 

No comments: