Java-Extracting Characters

Extracting Characters

Remember that the charAt( ) method of the StringBuffer class is helpful in extracting a single character from an object of the StringBuffer class. Sometimes, we may wish to extract more than one character at a time from a string. We shall use the getChars( ) method of the StringBuffer class in all such situations. For example, in the following program, the characters starting from the position ‘4’ and ending with the position ‘8-1′ will be extracted from the string ‘str’ and then stored in the character array ‘chr’ from the position ‘0’ in the array ‘chr’.


Program Source

public class Javaapp {
    public static void main(String[] args) {
        StringBuffer str = new StringBuffer();
        char chr[ ] = new char[4];
        str.getChars(4, 8, chr, 0);
        System.out.println("chr contents : "+String.valueOf(chr));

Leave a Comment