import java.lang.StringBuffer; public class Example { public static void main(String[] args) { StringBuffer str = new StringBuffer("Hello World!"); System.out.println("Before setCharAt operation: " + str); str.setCharAt(6, ','); System.out.println("After setCharAt operation: " + str); } }
Before setCharAt operation: Hello World! After setCharAt operation: Hello,World!
package org.apache.commons.lang3; public class Example { public static void main(String[] args) { StringBuffer str = new StringBuffer("abcde"); str.setCharAt(2, 'B'); System.out.println(str); } }
abBdeIn this example, we import the package "org.apache.commons.lang3" which contains the StringBuffer class. We create a new StringBuffer object with the string "abcde" and use the setCharAt() method to replace the character at index 2 with a capital B. The output shows the modified string. The package library used in this example is "org.apache.commons.lang3".