Sunday, 6 July 2014

deleteCharAt (int index)

public synchronized StringBuffer deleteCharAt(int index)
Removes the char at the specified position in this sequence.

public class StringBufferDelectCharAt {
     public static void main(String args[]){
        StringBuffer str1 = new StringBuffer("Hi How are you");
        
        System.out.println("str1 before calling delete method");
        System.out.println(str1);
        
        System.out.println("str1 after calling deleting spaces");
        str1.deleteCharAt(2);
        str1.deleteCharAt(5);
        str1.deleteCharAt(8);
        System.out.println(str1);
    }   
}

Output
str1 before calling delete method
Hi How are you
str1 after calling deleting spaces
HiHowareyou

1. Throws StringIndexOutOfBoundsException if the index is negative or greater than or equal to length().




Prevoius                                                 Next                                                 Home

delete (int start, int end)

public synchronized StringBuffer delete(int start, int end)
Removes the characters in a substring of this sequence. The substring begins at the specified start and extends to the character at index end – 1.

public class StringBufferDelete {
    public static void main(String args[]){
        StringBuffer str1 = new StringBuffer("Hi How are you");
        
        System.out.println("str1 before calling delete method");
        System.out.println(str1);
        
        System.out.println("str1 after calling delete method");
        str1.delete(2,10);
        System.out.println(str1);
    }
}

Output
str1 before calling delete method
Hi How are you
str1 after calling delete method
Hi you

1. Throws StringIndexOutOfBoundsException - if start is negative, greater than length(), or greater than end.



Prevoius                                                 Next                                                 Home

Friday, 4 July 2014

codePointCount (int beginIndex, int endIndex)

public synchronized int codePointCount(int beginIndex, int endIndex)
Returns the number of Unicode code points in the specified text range of this sequence.

public class StringBufferCodePointCount {
    public static void main(String args[]){
        int arr[] = {65536, 65537, 65538};
        String s = new String(arr, 0, arr.length);
        
        StringBuffer str = new StringBuffer(s);
        
        System.out.print("Code points count from 0 to " + str.length());
        System.out.println(" is "+ str.codePointCount(0, str.length()));
    }
}

Output
Code points count from 0 to 6 is 3





Prevoius                                                 Next                                                 Home

codePointBefore (int index)

public synchronized int codePointBefore(int index)
Returns the character (Unicode code point) before the specified index. The index refers to char values (Unicode code units) and ranges from 1 to length()

public class StringBufferCodePointBefore {
    public static void main(String args[]){
        int arr[] = {65536, 65537, 65538};
        String s = new String(arr, 0, arr.length);
        
        StringBuffer str = new StringBuffer(s);
        
        for(int i=1; i<str.length()+1; i++){
            System.out.print("Code point before index " + i +" is ");
            System.out.println(str.codePointBefore(i));
        }
    }
}

Output
Code point before index 1 is 55296
Code point before index 2 is 65536
Code point before index 3 is 55296
Code point before index 4 is 65537
Code point before index 5 is 55296
Code point before index 6 is 65538





Prevoius                                                 Next                                                 Home

codePointAt (int index)

public synchronized int codePointAt(int index)
Returns the character (Unicode code point) at the specified index. The index refers to char values (Unicode code units) and ranges from 0 to length()- 1.

public class StringBufferCodePointAt {
    public static void main(String args[]){
        int arr[] = {65536, 65537, 65538};
        String s = new String(arr, 0, arr.length);
        
        StringBuffer str = new StringBuffer(s);
        
        for(int i=0; i<str.length(); i++){
            System.out.print("Code point at index " + i +" is ");
            System.out.println(str.codePointAt(i));
        }
    }
}

Output
Code point at index 0 is 65536
Code point at index 1 is 56320
Code point at index 2 is 65537
Code point at index 3 is 56321
Code point at index 4 is 65538
Code point at index 5 is 56322





Prevoius                                                 Next                                                 Home

charAt (int index)

public synchronized char charAt(int index)
Returns the char value in this sequence at the specified index.

public class StringBufferCharAt {
    public static void main(String args[]){
        StringBuffer s1 = new StringBuffer("Hello");
        
        for(int i=0; i<s1.length(); i++){
            System.out.print("Character at index " + i + " is ");
            System.out.println(s1.charAt(i));
        }
    }
}

Output
Character at index 0 is H
Character at index 1 is e
Character at index 2 is l
Character at index 3 is l
Character at index 4 is o

1. Throws IndexOutOfBoundsException, if index is negative or greater than or equal to length().
public class StringBufferCharAtIndexOut {
    public static void main(String args[]){
        StringBuffer s1 = new StringBuffer("Hello");
        
        System.out.println("Character at index 5 is ");
        System.out.println(s1.charAt(5));
    }
}

Output
Character at index 5 is 
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
 at java.lang.StringBuffer.charAt(StringBuffer.java:204)
 at StringBufferCharAtIndexOut.main(StringBufferCharAtIndexOut.java:16)
Java Result: 1






Prevoius                                                 Next                                                 Home

capacity()

public synchronized int capacity()
The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur. Return the current capacity.

public class StringBufferCapacity {
    public static void main(String args[]){
        StringBuffer s1 = new StringBuffer("Hi");
        System.out.println("Capacity of s1 is " + s1.capacity());
    }
}

Output
Capacity of s1 is 18



Prevoius                                                 Next                                                 Home