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

public StringBuilder(CharSequence seq) 

Source Link

Document

Constructs a string builder that contains the same characters as the specified CharSequence .

Usage

From source file:Main.java

public static void main(String[] arg) {

    StringBuilder buffer = new StringBuilder("from java2s.com");

    buffer.append(buffer);/*w w  w .j a  v a  2s  .co  m*/

    System.out.println(buffer);
}

From source file:Main.java

public static void main(String[] args) {
    StringBuilder lipsum = new StringBuilder("Lorem ipsum dolor sit amet.");
    System.out.println("lipsum = " + lipsum.toString());

    lipsum.delete(0, 28);//from   www .  j  a v a  2s  .com
    System.out.println("lipsum = " + lipsum.toString());

    lipsum.deleteCharAt(lipsum.length() - 1);
    System.out.println("lipsum = " + lipsum.toString());
}

From source file:Main.java

public static void main(String[] a) {
    StringBuilder builder = new StringBuilder("Line 1\n");

    builder.append("Line 3\n");

    String lineToInsert = "Line 2\n";

    builder.insert(0, lineToInsert);/*ww  w . j  a va2s . c  o  m*/

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

}

From source file:Main.java

public static void main(String[] a) {
    StringBuilder builder = new StringBuilder("Line 1\n");

    builder.append("Line 3\n");

    builder.insert(0, "Line 2\n");

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

    builder = builder.delete(2, 6);// w  w  w  .j ava 2s . c  om

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

}

From source file:Main.java

public static void main(String[] args) {
    // Length is 5
    StringBuilder sb = new StringBuilder("Hello");

    // Now the length is 7 with last two characters as null character '\u0000'
    sb.setLength(7);/*  www  .  jav a2s  .com*/

    // Now the length is 2 and the content is "He"
    sb.setLength(2);

}

From source file:Main.java

public static void main(String[] arg) {

    String phrase = "abced";
    StringBuilder buffer = new StringBuilder(phrase);

    System.out.println(buffer);//from  w ww. j a  va  2s  .c  o m
}

From source file:Main.java

public static void main(String[] arg) {

    CharSequence phrase = "abced";
    StringBuilder buffer = new StringBuilder(phrase);

    System.out.println(buffer);/*from  w  ww.ja  v a 2s  . co m*/
}

From source file:Main.java

public static void main(String[] args) {
    // Create a String object
    String s1 = new String("Hello");

    // Create a StringBuilder from of the String object s1
    StringBuilder sb = new StringBuilder(s1);

    // Append " Java" to the StringBuilder's content
    sb.append(" Java"); // Now, sb contains "Hello Java"

    // Get a String from the StringBuilder
    String s2 = sb.toString(); // s2 contains "Hello Java"

}

From source file:Main.java

public static void main(String args[]) {

    // create an empty array list
    ArrayList<StringBuilder> arrlist1 = new ArrayList<StringBuilder>();

    // use add for new value
    arrlist1.add(new StringBuilder("from java2s.com"));

    // using clone to affect the objects pointed to by the references.
    ArrayList arrlist2 = (ArrayList) arrlist1.clone();

    // appending the string
    StringBuilder strbuilder = arrlist1.get(0);
    strbuilder.append("tutorials");

    System.out.println(arrlist1);

    System.out.println(arrlist2);
}

From source file:NavigableSetDemo.java

public static void main(String[] args) {
    List<Integer> list = Arrays.asList(3, 2, 4, 1, 5);
    NavigableSet<Integer> ns = new TreeSet<Integer>(list);
    System.out.println("Ascending order (default): " + ns);

    Iterator<Integer> descendingIterator = ns.descendingIterator();
    StringBuilder sb = new StringBuilder("Descending order: ");
    while (descendingIterator.hasNext()) {
        int m = descendingIterator.next();
        sb.append(m + " ");
    }// w  ww  .  j  a v a 2 s . c  o  m
    System.out.println(sb);

    int greatest = ns.lower(3);
    System.out.println("Lower of 3 = " + greatest);

    int smallest = ns.higher(3);
    System.out.println("Higher of 3 = " + smallest);
}