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) {
    String regex = "\\b\\d+\\b";
    StringBuffer sb = new StringBuffer();
    String replacementText = "";
    String matchedText = "";

    String text = "We have 7 tutorials for Java, 2 tutorials for Javascript and 1 tutorial for Oracle.";

    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(text);/*from w ww  .  j  ava 2s .  co  m*/

    while (m.find()) {
        matchedText = m.group();
        int num = Integer.parseInt(matchedText);
        if (num == 1) {
            replacementText = "only one";
        } else if (num < 5) {
            replacementText = "a few";
        } else {
            replacementText = "many";
        }
        m.appendReplacement(sb, replacementText);
    }

    m.appendTail(sb);
    System.out.println("Old  Text: " + text);
    System.out.println("New Text: " + sb.toString());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    NumberFormat numberFormat = NumberFormat.getPercentInstance();

    StringBuffer sb = new StringBuffer();
    numberFormat.format(111L, sb, new FieldPosition(0));
    System.out.println(sb);/*  w  w w. j a v a2  s. com*/

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    NumberFormat numberFormat = NumberFormat.getPercentInstance();

    StringBuffer sb = new StringBuffer();
    numberFormat.format(2.234, sb, new FieldPosition(0));
    System.out.println(sb);//from ww  w  .  j  ava2s  .  c o m

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    NumberFormat numberFormat = NumberFormat.getPercentInstance();

    StringBuffer sb = new StringBuffer();

    Object value = 11.111;// w  ww.ja v a 2 s. co m
    numberFormat.format(value, sb, new FieldPosition(NumberFormat.FRACTION_FIELD));
    System.out.println(sb);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    NumberFormat numberFormat = NumberFormat.getPercentInstance();

    StringBuffer sb = new StringBuffer();

    Object value = 111L;/*from  w w w  . j ava 2  s  . c  om*/
    numberFormat.format(value, sb, new FieldPosition(0));
    System.out.println(sb);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DateFormat dateFormat = DateFormat.getDateInstance();
    StringBuffer sb = new StringBuffer();

    sb = dateFormat.format(new Date(), sb, new FieldPosition(DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD));
    System.out.println(sb);/*from  w  w w .  j a va2  s .  c om*/

}

From source file:MatcherAppendReplacementGroupExample.java

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

    String candidateString = "My name is Bond. James Bond.";

    String replacement = "$1 Waldo $2";

    Matcher matcher = p.matcher(candidateString);
    matcher.find();// www.  j  a v a  2 s .c  o m

    matcher.appendReplacement(sb, replacement);

    String msg = sb.toString();
    System.out.println(msg);
}

From source file:Main.java

public static void main(String[] arguments) throws Exception {
    StringBuffer output = new StringBuffer();

    FileReader file = new FileReader("a.htm");
    BufferedReader buff = new BufferedReader(file);
    boolean eof = false;
    while (!eof) {
        String line = buff.readLine();
        if (line == null)
            eof = true;//from  www .ja  v a2  s .  c  om
        else
            output.append(line + "\n");
    }
    buff.close();

    String page = output.toString();
    Pattern pattern = Pattern.compile("<a.+href=\"(.+?)\"");
    Matcher matcher = pattern.matcher(page);
    while (matcher.find()) {
        System.out.println(matcher.group(1));
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    SimpleDateFormat formatter = new SimpleDateFormat();

    StringBuffer sb = new StringBuffer();
    formatter.format(new Date(), sb, new FieldPosition(DateFormat.DATE_FIELD));
    System.out.println(sb);/*ww w . j a va 2  s  .  co  m*/
}

From source file:Main.java

public static void main(String[] args) {
    String input = " javaId=2 test test javaId=33432. javaId=100";

    Pattern p = Pattern.compile("(javaId=)(\\d+)");
    Matcher m = p.matcher(input);

    StringBuffer result = new StringBuffer();
    while (m.find()) {
        System.out.println("Masking: " + m.group(2));
        m.appendReplacement(result, m.group(1) + "***masked***");
    }/*from www .  j a  va 2  s  .c  o m*/
    m.appendTail(result);
    System.out.println(result);
}