Sunday, 31 December 2017

Methods on String Object in Java Part-2


public class StringMethodsTest2 {

    public static void main(String[] args) {
        String test="java programme with string";
        //getBytes---returns byte array of string. it returns sequence of bytes
        byte[] sArray = test.getBytes();
        for (int i = 0; i < sArray.length; i++) {
            System.out.println(sArray[i]);
        }
        System.out.println(new String(sArray));
       
        //getChars() --does not return any value but we can copy content of string to an char array
        // it required 4 iput parameters sArray
        //srcBegin index of the first character in the string to copy.--1
        //srcEnd index after the last character in the string to copy.--2
        //dst the destination array.--3
        //dstBegin the start offset in the destination array.--4
        char[] characters = new char[4];
        test.getChars(0, 4, characters, 0);//char array get chars from 0 to 3 from string testi.e it has java
        for (int i = 0; i < characters.length; i++) {
            System.out.println("characteer array is" + characters[i]);
        }
       
    //indexof(char c)-----returns  index value of given char in string
    //and returns-1 if char is not found in string
        //it will pring first occurence of index if given char is repeated in string
        System.out.println(test.indexOf("a"));//1
       
        //indexof(char c, int fromIndex)-----returns  index value of given char in string from given index
        //and returns-1 if char is not found in string
            //it will pring first occurence of index if given char is repeated in string
            System.out.println(test.indexOf("a",2));//3
           
            //indexof(String subString)-----returns  index value of given subString
            //and returns-1 if char is not found in string
                System.out.println(test.indexOf("va"));//2
               
                //indexof(String subString, int fromIndex)-----returns  index value of given subString from given index
                //and returns-1 if char is not found in string
                    System.out.println(test.indexOf("va",4));//-1
    }

}

No comments:

Post a Comment