Write a Java program to practice using String Buffer class and its methods.
Program Code:
import java.lang.String;
class stringbufferdemo
{
public static void main(String arg[])
{
StringBuffer sb=new StringBuffer("This is my college");
System.out.println("This string sb is : " +sb);
System.out.println("The length of the string sb is : " +sb.length());
System.out.println("The capacity of the string sb is : " +sb.capacity());
System.out.println("The character at an index of 6 is : " +sb.charAt(6));
sb.setCharAt(3,'x');
System.out.println("After setting char x at position 3 : " +sb);
System.out.println("After appending : " +sb.append(" in gulbarga "));
System.out.println("After inserting : " +sb.insert(19,"gpt "));
System.out.println("After deleting : " +sb.delete(19,22));
}
}
Output:
This string sb is : This is my college
The length of the string sb is : 18
The capacity of the string sb is : 34
The character at an index of 6 is : s
After setting char x at position 3 : Thix is my college
After appending : Thix is my college in gulbarga
After inserting : Thix is my college gpt in gulbarga
After deleting : Thix is my college in gulbarga
Post A Comment:
0 comments: