Wednesday, 9 July 2014

replace (int start, int end, String str)

public synchronized StringBuffer replace(int start, int end, String str)
Replaces the characters in a substring of this sequence with characters in the specified String. . The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence if no such character exists. First the characters in the substring are removed and then the specified String is inserted at start.

public class StringBufferReplace {
    public static void main(String args[]){
        StringBuffer str1 = new StringBuffer("Hello Buddy, How are you");
        StringBuffer str3 = new StringBuffer("Hello Buddy, How are you");
        
        String str2 = "Dude, I am fine";
        String str4 = "D";
        
        System.out.println("str1 before replcaement: " + str1);
        str1.replace(6, 11, str2);
        System.out.println("str1 After replcaement: " + str1);
        
        System.out.println("str3 before replcaement: " + str3);
        str3.replace(6, 11, str4);
        System.out.println("str3 After replcaement: " + str3);
    }
}

Output
str1 before replcaement: Hello Buddy, How are you
str1 After replcaement: Hello Dude, I am fine, How are you
str3 before replcaement: Hello Buddy, How are you
str3 After replcaement: Hello D, How are you

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






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment