StringBuffer to String

In this chapter you will learn:

  1. How to convert portion of a StringBuffer to String
  2. How to convert StringBuffer to String

Portion of a StringBuffer to String

You can obtain a portion of a StringBuffer by calling substring( ).

The first form returns the substring that starts at startIndex and runs to the end of the invoking StringBuffer object. The second form returns the substring that starts at startIndex and runs through endIndex-1.

  • String substring(int start)
    Returns a String that contains a subsequence of characters.
  • String substring(int start, int end)
    Returns a String that contains a subsequence of characters.
  • CharSequence subSequence(int start, int end)
    Returns a subsequence of this sequence.
public class Main {
  public static void main(String[] argv) {
    StringBuffer sb = new StringBuffer();
    sb.append("java2s.com");
    String sub = sb.substring(1,2);/*from j a  va2s.c  o  m*/
    System.out.println(sub);
    System.out.println(sb.toString());
  }
}

The output:

Convert StringBuffer to String

String toString() returns a string representing the data in this sequence.

public class Main {
  public static void main(String[] argv) {
    StringBuffer sb = new StringBuffer();
    sb.append("java2s.com");
    //from   j  a  v  a  2s .  co m
    System.out.println(sb.toString());

  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. What is the difference between StringBuilder and StringBuffer
  2. How to StringBuilder's Constructors to create StringBuilder
  3. How to create StringBuilder with initial String value
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