Example usage for java.lang StringBuilder StringBuilder

List of usage examples for java.lang StringBuilder StringBuilder

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public StringBuilder() 

Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] a) {
    StringBuilder builder1 = new StringBuilder();
    StringBuilder builder2 = new StringBuilder(0);
    StringBuilder builder3 = new StringBuilder(100);

    System.out.println(builder1.capacity());
    System.out.println(builder2.capacity());
    System.out.println(builder3.capacity());

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    StringBuilder buf = new StringBuilder(); // Buffer to hold output
    Formatter formatter = new Formatter(buf); // Formatter to format data into
                                              // buf
    formatter.format("%4d)%+7.2f", 2, 1234.1234);
    System.out.println(buf);// w w w .  j a v a2s.  c  o m

}

From source file:Main.java

public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    Formatter fm = new Formatter(sb);

    // Formatting strings
    fm.format("%1$s, %2$s,  and  %3$s %n", "A", "B", "C");
    fm.format("%3$s, %2$s,  and  %1$s %n", "D", "E", "F");

    // Formatting numbers
    fm.format("%1$4d, %2$4d, %3$4d %n", 1, 10, 100);
    fm.format("%1$4d, %2$4d, %3$4d %n", 10, 100, 1000);
    fm.format("%1$-4d, %2$-4d,  %3$-4d %n", 1, 10, 100);
    fm.format("%1$-4d, %2$-4d,  %3$-4d %n", 10, 100, 1000);

    // Formatting date and time
    Date dt = new Date();
    fm.format("Today  is %tD  %n", dt);
    fm.format("Today  is %tF %n", dt);
    fm.format("Today  is %tc %n", dt);

    // Display the entire formatted string
    System.out.println(sb.toString());

}

From source file:Main.java

public static void main(String[] args) {
    // Create an empty StringBuffer
    StringBuilder sb = new StringBuilder();
    printDetails(sb);/*from w  w w . ja va2  s . com*/

    // Append "good"
    sb.append("good");
    printDetails(sb);

    // Insert "Hi " in the beginning
    sb.insert(0, "Hi ");
    printDetails(sb);

    // Delete the first o
    sb.deleteCharAt(1);
    printDetails(sb);

    // Append "  be  with  you"
    sb.append(" be  with  you");
    printDetails(sb);

    // Set the length to 3
    sb.setLength(3);
    printDetails(sb);

    // Reverse the content
    sb.reverse();
    printDetails(sb);
}

From source file:Main.java

public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 100; i++) {
        sb.append("this is a test. ");
    }//ww  w  .  j ava  2  s.c o  m
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextPane newsTextPane = new JTextPane();
    newsTextPane.setContentType("text/html");
    newsTextPane.setEditable(false);
    newsTextPane.setText(sb.toString());

    JScrollPane scrollPane = new JScrollPane(newsTextPane);
    scrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
    frame.add(scrollPane);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    String input = "this is a test";
    StringBuilder result = new StringBuilder();
    char currentCharacter;
    int count;/*from ww w . j  a v a 2s  .  co m*/

    for (int i = 0; i < input.length(); i++) {
        currentCharacter = input.charAt(i);
        count = 1;
        while (i < input.length() - 1 && input.charAt(i + 1) == currentCharacter) {
            count++;
            i++;
        }
        result.append(currentCharacter);
        result.append(count);
    }

    System.out.println("" + result);
}

From source file:Main.java

public static void main(String[] args) {
    final StringBuilder sb = new StringBuilder();
    sb.append("<html>");
    sb.append("<body><ol>");
    Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
    for (Font font : fonts) {
        String name = font.getName();
        sb.append("<li style='font-family: " + name + "; font-size: 20px;'>");
        sb.append(name);//  w ww .  java  2 s  . c  om
    }

    JScrollPane sp = new JScrollPane(new JLabel(sb.toString()));
    Dimension d = sp.getPreferredSize();
    sp.setPreferredSize(new Dimension(d.width, 150));
    JOptionPane.showMessageDialog(null, sp);
}

From source file:Main.java

public static void main(String[] args) {
    String input = "ThisIsATest";

    // split into words
    String[] words = input.split("(?=[A-Z])");

    words[0] = capitalizeFirstLetter(words[0]);

    // join/*from   w  w  w  . j  a v a 2  s  . c o m*/
    StringBuilder builder = new StringBuilder();
    for (String s : words) {
        builder.append(s).append(" ");
    }

    System.out.println(builder.toString());

}

From source file:Main.java

public static void main(String[] argv) {
    JEditorPane jep = new JEditorPane();
    jep.setContentType("text/html");
    StringBuilder sb = new StringBuilder();
    sb.append("<b>Welcome</b>:<br><hr>");
    for (int i = 1; i <= 3; i++) {
        sb.append(create(i));// ww  w .  j a v a  2 s .c  om
    }
    sb.append("<hr>");
    jep.setText(sb.toString());
    jep.setEditable(false);
    jep.addHyperlinkListener(e -> {
        if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
            System.out.println(e.getURL());
            Desktop desktop = Desktop.getDesktop();
            try {
                desktop.browse(e.getURL().toURI());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(jep);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    String input = "This is a sentence";
    char[] charinput = input.toCharArray();
    Stack<String> stack = new Stack<String>();
    for (int i = input.length() - 1; i >= 0; i--) {
        stack.push(String.valueOf(charinput[i]));
    }/*from   ww  w  .  j a  v  a2s .co m*/
    StringBuilder StackPush = new StringBuilder();
    for (int i = 0; i < stack.size(); i++) {
        StackPush.append(stack.get(i));
    }
    System.out.println(StackPush.toString());
}