Java StringBuffer update String char value

Introduction

We can use the following method to get and set char value in a StringBuffer.

Their general forms are shown here:

char charAt(int where)  
void setCharAt(int  where, char ch) 
// Demonstrate charAt() and setCharAt().
public class Main {
  public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("Hello");
    System.out.println("buffer before = " + sb);
    System.out.println("charAt(1) before = " + sb.charAt(1));
      sb.setCharAt(1, 'i');
      sb.setLength(2);/*ww w .ja v a2s.c o  m*/
      System.out.println("buffer after = " + sb);
      System.out.println("charAt(1) after = " + sb.charAt(1));
  }
}



PreviousNext

Related