Monday, 7 July 2014

insert (int offset, char[] str)

public synchronized StringBuffer insert(int offset, char[] str)
Inserts the string representation of the char array argument into this sequence at the position indicated by offset.

public class StringBufferInsertCharArray {
    public static void main(String args[]){
        char c[] = {'H', 'e', 'l', 'l', 'o', ','};
        StringBuffer str = new StringBuffer("How are You");
        str.insert(0, c);
        System.out.println("str = " + str);
    }
}

Output
str = Hello,How are You

1. Throws StringIndexOutOfBoundsException, if the offset is invalid.



Prevoius                                                 Next                                                 Home

insert (int offset, char c)

public synchronized StringBuffer insert(int offset, char c)
Inserts the string representation of the char argument into this sequence at the indicated offset.

public class StringBufferInsert {
    public static void main(String args[]){
        StringBuffer str = new StringBuffer("String str is ");
        str.insert(str.length(), 'a');
        System.out.println(str);
    }
}

Output
String str is a


Prevoius                                                 Next                                                 Home

insert (int offset, boolean b)

public StringBuffer insert(int offset, boolean b)
Inserts the string representation of the boolean argument into this sequence at the indicated offset.

public class StringBufferInsert {
    public static void main(String args[]){
        StringBuffer str = new StringBuffer("String str is ");
        str.insert(str.length(), true);
        System.out.println(str);
    }
}

Output
String str is true



Prevoius                                                 Next                                                 Home

indexOf (String str, int fromIndex)

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

public class StringBufferIndexOf1 {
    public static void main(String args[]){
        StringBuffer str1 = new StringBuffer("abcbabbcdabc");
        System.out.print("Index of \"ab\" from index 0 ");
        System.out.println(str1.indexOf("ab", 0));
        System.out.print("Index of \"ab\" from index 1 ");
        System.out.println(str1.indexOf("ab", 1));
    }     
}

Output
Index of "ab" from index 0 0
Index of "ab" from index 1 4

1. Throws NullPointerException, if str is null.




Prevoius                                                 Next                                                 Home

Sunday, 6 July 2014

indexOf (String str)

public int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.

public class StringBufferIndexOf {
     public static void main(String args[]){
        StringBuffer str1 = new StringBuffer("Hi How are you");
        System.out.print("Index of \"How\" is ");
        System.out.println(str1.indexOf("How"));
    }           
}

Output
Index of "How" is 3



 
Prevoius                                                 Next                                                 Home

getChars (int srcBegin, int srcEnd, char[] dst, int dstBegin)

public synchronized void getChars(int srcBegin, int srcEnd, char[] dst,
int dstBegin)
Characters are copied from this sequence into the destination character array dst. The first character to be copied is at index srcBegin; the last character to be copied is at index srcEnd-1. The characters are copied into the subarray of dst starting at index dstBegin and ending at index.

public class StringBufferGetChars {
     public static void main(String args[]){
        StringBuffer str1 = new StringBuffer("Hi How are you");
        char str2[] = new char[20];
        str2[17]='e';
        str2[18] = 'n';
        str2[19] = 'd';
        
        str1.getChars(2, str1.length(), str2, 2);
        for(char i : str2){
            System.out.print(i);
        }
    }       
}

Output
   How are you   end

1. Throws IndexOutOfBoundsException - if any of the following is true:
     a. srcBegin is negative
     b. dstBegin is negative
     c. the srcBegin argument is greater than the srcEnd argument.
     d. srcEnd is greater than this.length().


Prevoius                                                 Next                                                 Home

ensureCapacity (int minimumCapacity)

public synchronized void ensureCapacity(int minimumCapacity)
Ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of:
   1. The minimumCapacity argument.
   2. Twice the old capacity, plus 2. 


public class StringBufferEnsureCapacity {
     public static void main(String args[]){
        StringBuffer str1 = new StringBuffer("Hi How are you");
        
        System.out.println("str1 capacity is");
        System.out.println(str1.capacity());
        
        System.out.println("str1 capacity is");
        str1.ensureCapacity(50);
        System.out.println(str1.capacity());
    }       
}

Output
str1 capacity is
30
str1 capacity is
62





Prevoius                                                 Next                                                 Home