StringBuffer char operation

In this chapter you will learn:

  1. How to get a char from a certain index inside a StringBuffer
  2. How copy part of a StringBuffer to a char array
  3. How to update a char in a StringBuffer

Get a char from a certain index inside a StringBuffer

The value of a single character can be obtained from a StringBuffer via the charAt( ) method. char charAt(int index) returns the char value in this sequence at the specified index.

public class Main {
  public static void main(String[] argv) {
    StringBuffer sb = new StringBuffer();
    sb.append("java2s.com");
    //from ja v  a 2 s.com
    char ch =  sb.charAt(0);
    
    System.out.println(ch);

  }
}

The output:

Copy to a char array

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) copies characters from StringBuffer into the destination character array dst.

public class Main {
  public static void main(String args[]) {
    StringBuffer buffer = new StringBuffer("hello there");
/*ja v a2 s.co m*/
    System.out.printf("buffer = %s\n", buffer.toString());
    System.out.printf("Character at 0: %s\nCharacter at 4: %s\n\n", buffer.charAt(0), buffer
        .charAt(4));

    char charArray[] = new char[buffer.length()];
    buffer.getChars(0, buffer.length(), charArray, 0);
    System.out.print("The characters are: ");

  }
}

Update char in a StringBuffer

You can set the value of a character within a StringBuffer using setCharAt( ).

void setCharAt(int index, char ch) set character at the specified index is set to ch.

The following example demonstrates charAt( ) and setCharAt( ):

public class Main {
  public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("java2s.com");
    System.out.println("buffer before = " + sb);
    System.out.println("charAt(1) before = " + sb.charAt(1));
//from  ja  v  a 2 s  .c o  m
    sb.setCharAt(1, 'i');
    sb.setLength(2);
    System.out.println("buffer after = " + sb);
    System.out.println("charAt(1) after = " + sb.charAt(1));

  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. Replace string content in a StringBuffer
  2. Reverse a StringBuffer
  3. Delete characters within a StringBuffer
Home » Java Tutorial » String

String

    Java String type
    Java String Concatenation
    Java String Creation
    Java String Compare
    Java String Search
    Java String and char array
    Java String Conversion
    String trim, length, is empty, and substring
    String replace

StringBuffer

    StringBuffer class
    StringBuffer Insert and Append
    StringBuffer length and capacity
    StringBuffer char operation
    StringBuffer Operations
    Search within StringBuffer
    StringBuffer to String

StringBuilder

    StringBuilder
    StringBuilder insert and append
    StringBuilder length and capacity
    StringBuilder get,delete,set char
    StringBuilder delete, reverse
    StringBuilder search with indexOf and lastIndexOf
    StringBuilder to String

String Format

    Formatter class
    Format Specifier
    Format String and characters
    Format integer value
    Format decimal
    Scientific notation format
    Format octal and hexadecimal value
    Format date and time value
    Escape Formatter
    Minimum Field Width
    Specifying Precision
    Format Flags
    Uppercase Option
    Formatter Argument Index
    Align left and right
    Left and right padding a string

String Format Utilities

    Abbreviate string
    Caplitalize a string
    Uncapitalize a string
    Utility class for right padding
    Left padding
    Centers a String
    Transforms words