Menu
Topics Index
...
`

StringBufferdelete and deleteCharAt Demo - Java Example Program


StringBufferdelete and deleteCharAt Demo
class StringBufferDeleteExample
{
    public static void main(String arg[])
    {
        StringBuffer firstWord = new StringBuffer("Bharat");
        System.out.println(firstWord);
        firstWord.delete(1, 4);
        System.out.println(firstWord);
        firstWord.deleteCharAt(0);
        System.out.println(firstWord);    
    }
}
OUTPUT

Bharat
Bat
at

DESCRIPTION

This program shows how to delete a character or set of characters from a StringBuffer object. The first output line Bharat is just the display of contents of firstWord. The second output line Bat is obtained by deleting a substring from Bharat using delete(startIndex, endIndex). The substring(1, 4) i.e har is deleted from Bharat to get Bat. The third output at is obtained by deleting the first character of Bat using deleteCharAt(0). The character at zero position is deleted.

THINGS TO TRY
  • Write this code at the end of above program and observe the output. The output is should be a. Since characters from 1 are deleted
    firstWord.delete(1, 4);</br>
    System.out.println(firstWord);

© meritcampus 2019

All Rights Reserved.

Open In App