StringBuilder insert and append
In this chapter you will learn:
- How to append data to the end of a StringBuilder
- How to chain append methods together
- How to insert data to the middle of a StringBuilder
- Combine append and insert methods
Append data to the end
StringBuilder
has the following methods to
append data to the end of a StringBuilder
.
StringBuilder append(boolean b)
StringBuilder append(char c)
StringBuilder append(char[] str)
StringBuilder append(char[] str, int offset, int len)
StringBuilder append(CharSequence s)
StringBuilder append(CharSequence s, int start, int end)
StringBuilder append(double d)
StringBuilder append(float f)
StringBuilder append(int i)
StringBuilder append(long lng)
StringBuilder append(Object obj)
StringBuilder append(String str)
StringBuilder append(StringBuffer sb)
StringBuilder appendCodePoint(int codePoint)
public class Main {
public static void main(String[] argv) {
StringBuilder sb = new StringBuilder();
sb.append("java2s.com");
sb.append(123);/*j a v a 2 s .c om*/
sb.append(1.23);
sb.append(true);
System.out.println(sb.toString());
}
}
The output:
Chain append methods together
Each append
methods from StringBuilder
return an
instance of current StringBuilder
.
We can chain the append
methods together.
public class Main {
/* ja v a2 s .c om*/
public static void main(String[] argv) {
String s1 = "Hello" + ", " + "World";
System.out.println(s1);
// Build a StringBuilder, and append some things to it.
StringBuilder sb2 = new StringBuilder();
sb2.append("Hello");
sb2.append(',');
sb2.append(' ');
sb2.append("World");
StringBuilder sb3 = new StringBuilder().append("Hello").append(',').append(' ').append("World");
System.out.println(sb3.toString());
}
}
The output:
Insert data to the middle of a StringBuilder
With the following insert
methods from StringBuilder
we can insert value to the middle of a StringBuilder
.
StringBuilder insert(int offset, boolean b)
StringBuilder insert(int offset, char c)
StringBuilder insert(int offset, char[] str)
StringBuilder insert(int index, char[] str, int offset, int len)
StringBuilder insert(int dstOffset, CharSequence s)
StringBuilder insert(int dstOffset, CharSequence s, int start, int end)
StringBuilder insert(int offset, double d)
StringBuilder insert(int offset, float f)
StringBuilder insert(int offset, int i)
StringBuilder insert(int offset, long l)
StringBuilder insert(int offset, Object obj)
StringBuilder insert(int offset, String str)
The following code inserts three lines of string to the
StringBuilder
. The new line escape string \n
is used.
public class Main {
public static void main(String[] a) {
StringBuilder builder = new StringBuilder("Line 1\n");
//j av a 2 s . c o m
builder.append("Line 3\n");
String lineToInsert = "Line 2\n";
builder.insert(0, lineToInsert);
System.out.println(builder.toString());
}
}
Output:
Combine append and insert methods
public class Main {
public static void main(String[] argv) {
StringBuilder sb = new StringBuilder();
sb.append("java2s.com");
sb.append(123);// j av a 2s . c o m
sb.append(1.23);
sb.append(true);
sb.insert(4,"JAVA2S.COM");
System.out.println(sb.toString());
}
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » String