Example usage for java.lang StringBuffer StringBuffer

List of usage examples for java.lang StringBuffer StringBuffer

Introduction

In this page you can find the example usage for java.lang StringBuffer StringBuffer.

Prototype

@HotSpotIntrinsicCandidate
public StringBuffer() 

Source Link

Document

Constructs a string buffer with no characters in it and an initial capacity of 16 characters.

Usage

From source file:Main.java

public static void main(String[] args) {

    StringBuffer buffer = new StringBuffer();
    Formatter formatter = new Formatter(buffer, Locale.US);

    // format a new string
    String name = "from java2s.com";
    formatter.format("Hello %s !", name);

    // print the formatted string with default locale
    System.out.println(formatter);

    // print the formatter as a string
    System.out.println(formatter.toString());
}

From source file:Main.java

public static void main(String[] args) {

    StringBuffer buffer = new StringBuffer();
    Formatter formatter = new Formatter(buffer, Locale.US);

    // format a new string
    String name = "from java2s.com";
    formatter.format("Hello %s !", name);

    // print the formatted string
    System.out.println(formatter);

    // flush the formatter. Here it does nothing.
    formatter.flush();/*from  w w  w. j av a2s.  c  o m*/
    System.out.println("Formatter Flushed.");
}

From source file:Main.java

public static void main(String[] args) {

    StringBuffer buffer = new StringBuffer();
    Formatter formatter = new Formatter(buffer);

    // format a new string
    String name = "from java2s.com";
    formatter.format("Hello %s !", name);

    // print the formatted string
    System.out.println(formatter);

    // flush the formatter. Here it does nothing.
    formatter.flush();//from  w w w  . j a  v  a 2 s  .c  o m
    System.out.println("Formatter Flushed.");
}

From source file:Main.java

public static void main(String[] args) {

    StringBuffer buffer = new StringBuffer();
    Formatter formatter = new Formatter(buffer, Locale.US);

    // format a new string
    String name = "from java2s.com";
    formatter.format("Hello %s !", name);

    // print the formatted string
    System.out.println(formatter);

    // close the formatter
    formatter.close();/*from  w ww . ja  v  a2s .c  o  m*/

    // attempt to access the formatter results in exception
    System.out.println(formatter);
}

From source file:Main.java

public static void main(String[] args) {
    boolean b = true;
    StringBuffer sb1 = new StringBuffer();
    sb1.append(b);//w  w w  . jav a 2 s  .c  o  m
    System.out.println(sb1);

    char c = 'Y';
    sb1.append(c);
    System.out.println(sb1);

    char[] c1 = new char[] { 'Y', 'e', 's' };
    sb1.append(c1);

    System.out.println(sb1);

    double d = 1.0;
    sb1.append(d);
    System.out.println(sb1);

    float f = 1.0f;
    sb1.append(f);
    System.out.println(sb1);

    int i = 1;
    sb1.append(i);
    System.out.println(sb1);

    long l = 1;
    sb1.append(l);

    System.out.println(sb1);

    Object obj = new String("Yes");
    sb1.append(obj);
    System.out.println(sb1);

    String str = new String("Yes");
    sb1.append(str);
    System.out.println(sb1);
}

From source file:Main.java

public static void main(String[] args) {
    String str = "this is a test";

    StringBuffer sb = new StringBuffer();
    Matcher m = Pattern.compile("([a-z])([a-z]*)", Pattern.CASE_INSENSITIVE).matcher(str);
    while (m.find()) {
        m.appendReplacement(sb, m.group(1).toUpperCase() + m.group(2).toLowerCase());
    }/*from  ww  w  .  j  a v a2  s.c o  m*/
    System.out.println(m.appendTail(sb).toString());
}

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("test");
    StringBuffer sb = new StringBuffer();

    String candidateString = "This is a test.";

    String replacement = "Test";
    Matcher matcher = p.matcher(candidateString);
    matcher.find();/*from  www .j  a  v a  2  s .c  om*/

    matcher.appendReplacement(sb, replacement);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File file = new File("C:/String.txt");
    StringBuffer strContent = new StringBuffer();
    FileInputStream fin = new FileInputStream(file);
    int ch;/*w ww.j  a  va 2s.  com*/
    while ((ch = fin.read()) != -1) {
        strContent.append((char) ch);
    }
    fin.close();
    System.out.println(strContent);
}

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("(another) (test)");
    StringBuffer sb = new StringBuffer();

    String candidateString = "This is another test.";

    String replacement = "$1 AAA $2";
    Matcher matcher = p.matcher(candidateString);
    matcher.find();//from   ww  w.  j a  va 2 s .c  o m

    matcher.appendReplacement(sb, replacement);
    String msg = sb.toString();
    System.out.println(msg);
}

From source file:MainClass.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("(\\w+) (\\w+)");
    StringBuffer sb = new StringBuffer();

    String candidateString = "Jack Lee";

    String replacement = "$2, $1";
    Matcher matcher = p.matcher(candidateString);
    matcher.matches();//from w  w  w .ja  v  a 2 s.  co  m

    matcher.appendReplacement(sb, replacement);

    System.out.println(sb.toString());
}