Java-Extracting Character

Extracting Character

The charAt( ) method of StringBuffer class enables us to extract at the specified index position. For example, in the following program, the character at the index position ‘3’ in the string “ABCDE” is ‘D’, the output statement will return the character ‘D’.

The setCharAt( ) method of StringBuffer class enables us to replace an existing character at an index position by a new character. For example, in the following program, the character at the index position ‘3’ in the string “Prorram” will be replaced by the character ‘g’. As a result, the output statement will display the string “Program”.

Program

final2

Program Source

public class Javaapp {
    
    public static void main(String[] args) {
        
        StringBuffer str1 = new StringBuffer("ABCDE");    
        System.out.println("str1 3th character : "+str1.charAt(3));
        
        StringBuffer str2 = new StringBuffer("Prorram");
        str2.setCharAt(3, 'g');
        System.out.println("str2 content : "+str2);
    }
}

Leave a Comment