Page 310 - ComputerScience_Class_11
P. 310
The output of the preceding program is as follows:
BlueJ: Terminal Window - Java
Options
ComputerScience
Output:
ComputerScience
The setCharAt() Method
We use the setCharAt() method to replace one character with another character. The syntax of the setCharAt() method is:
StringBuffer string_object.setCharAt(index value,Character);
Let us see the below example:
Write a program to replace a character at a specific index in a StringBuffer and print the
Program 24
result.
1 class characterReplace
2 {
3 public static void main(String[] args)
4 {
5 StringBuffer s1= new StringBuffer("Computer");
6 s1.setCharAt(2,'M');
7 System.out.println(s1);
8 }
9 }
The output of the preceding program is as follows:
BlueJ: Terminal Window - Java
Options
CoMputer
The insert() Method
We use the insert() method to insert a string at the specified index into another string. The syntax of the insert() method is:
StringBuffer string_object.index(index value,StringBuffer Variable);
Let us see the below example:
Program 25 Write a program to insert a string at a specific index in a StringBuffer and print the result.
1 class stringInsert
2 {
308 Touchpad Computer Science (Ver. 3.0)-XI

