This article helps you to learn some important methods used in the Java StringBuffer() class with a variety of examples.
Before reading further, read the previous part of this series.
Now in this lecture, we will learn about an important Java StringBuffer() class method in detail, StringBuffer append().
Method: Java StringBuffer append()
Basically this method appends the specified string with the given string. It creates a new StringBuffer initialized to the contents of the specified string.
It is also overloaded by the following parameters:
- StringBuffer append(String str)
- StringBuffer append(char c)
- StringBuffer append(char[] str)
- StringBuffer append(char[] str, int offset, int len)
- StringBuffer append(CharSequence s)
- StringBuffer append(CharSequence s, int start, int end)
- StringBuffer append(double d)
- StringBuffer append(float f)
- StringBuffer append(int i)
- StringBuffer append(long lng)
- StringBuffer append(Object obj)
- StringBuffer append(StringBuffer sb)
- StringBuffer appendCodePoint(int codePoint)
- ·StringBuffer append(boolean b)
We will explain all the overloaded parameters by this method, taking them into various groups with examples.
First, we will explain the top four from the preceding list, because it will be very effective in understanding and remembering the things if we move down slowly instead of moving quickly and it becomes hotchpotch.
StringBuffer append(String str) appends the specified string to the given character sequence. The characters of the string arguments are appended in order (thread-safe) increasing the length of the given sequence by the length of the argument. If the string str is null then only the four characters “null” will be appended.
StringBuffer append(char c) appends the string representation of the char (character) argument to the given sequence. The argument is appended to the contents of the given sequence by increasing its (sequence) length by 1.
StringBuffer append(char[] str) appends the string representation of the char array, in other words, string argument to the given sequence. The char array arguments are appended in order (thread-safe) to the contents of the given sequence and its (sequence) length increases by the length of argument.
StringBuffer append(char[] str, int offset, int len) appends the string representation of a subarray of char array argument to the given sequence. The characters of the char array str, starting at index offset are appended to the contents of the given sequence and its (sequence) length increases by the value of length len.
Let's see the code examples for all four, but one by one.
Example: append(String str)
- public class AppendStringMethod
- {
- public static void main(String args[])
- {
- StringBuffer str1 = new StringBuffer("Watch this ");
- StringBuffer str2 = new StringBuffer("1234");
- System.out.println("Output for append(String str):");
- System.out.println();
- System.out.println("Buffer is: " + str1);
- str1.append("later..!!");
- System.out.println("String str1 after append: " + str1);
- System.out.println("Buffer is: " + str2);
- str2.append("-5678");
- System.out.println("String str2 after append: " + str2);
- }
- }
Output
Example: append(char c)
- public class AppendCharMethod
- {
- public static void main(String args[])
- {
- StringBuffer str1 = new StringBuffer("Watch this ");
- StringBuffer str2 = new StringBuffer("1234");
- System.out.println("Output for append(char c):");
- System.out.println();
- System.out.println("Buffer is: " + str1);
- str1.append('L');
- System.out.println("String str1 after append: " + str1);
- System.out.println("Buffer is: " + str2);
- str2.append('%');
- System.out.println("String str2 after append: " + str2);
- }
- }
Output
Example: append(char[] str)
- public class AppendCharStrMethod
- {
- public static void main(String args[])
- {
- StringBuffer str1 = new StringBuffer("Watch this ");
- StringBuffer str2 = new StringBuffer("123");
- System.out.println("Output for append(char[] str):");
- System.out.println();
- System.out.println("Buffer is: " + str1);
- char[] str = new char[]
- {
- 'M', 'e', 't', 'h', 'o', 'd'
- };
- str1.append(str);
- System.out.println("String str1 after append: " + str1);
-
-
- System.out.println("Buffer is: " + str2);
- char[] strr = new char[]
- {
- '/', '3', '2', '1'
- };
- str2.append(strr);
- System.out.println("String str2 after append: " + str2);
-
-
- }
- }
Output
Example: append(char[] str, int offset, int len)
- public class AppendCharStrOffsetLengthMethod
- {
- public static void main(String args[])
- {
- StringBuffer str1 = new StringBuffer("Watch this ");
- StringBuffer str2 = new StringBuffer("123");
- System.out.println("Output for append(char[] str, int offset, int len):");
- System.out.println();
- System.out.println("Buffer is: " + str1);
- char[] str = new char[] {
- 'S', 'e', 'l', 'e', 'c', 't', 'i', 'o', 'n'
- };
- str1.append(str, 1, 5);
- System.out.println("String str1 after append: " + str1);
-
-
- System.out.println("Buffer is: " + str2);
- char[] strr = new char[] {
- '*', '4', '5', '6', '7'
- };
- str2.append(strr, 0, 3);
- System.out.println("String str2 after append: " + str2);
-
-
- }
- }
Output
Now the next two methods will be discussed here.
StringBuffer append(CharSequence s) appends the specified character sequence to the given character sequence. The characters of charSequence argument are appended in order (thread-safe) and increase its (sequence) length by the length of the argument.
StringBuffer append(CharSequence s, int start, int end) appends the subsequence of the specified character sequence to the given sequence. The characters of the argument s, starting at index (start) are appended in order (thread-safe) to the content of the given sequence until the (end) index and the length of the sequence depends on the values of start and end index.
It also throws the exception indexOutOfBoundsException() if the index of the start or end is negative or the start index is greater than the end index or the end index is greater than the length of CharSequence.
Both methods return the reference to the given object.
Example: append(CharSequence s)
- public class AppendCharSqncMethod
- {
- public static void main(String args[])
- {
- StringBuffer str1 = new StringBuffer("Come ");
- StringBuffer str2 = new StringBuffer("1A2B");
- System.out.println("Output for append(CharSequence s):");
- System.out.println();
- System.out.println("Buffer is: " + str1);
- CharSequence CS1 = "back";
- str1.append(CS1);
- System.out.println("String str1 after append: " + str1);
- System.out.println("Buffer is: " + str2);
- CharSequence CS2 = "=B2A1";
- str2.append(CS2);
- System.out.println("String str2 after append: " + str2);
- }
- }
Output
Example: append(CharSequence s, int start, int end)
- public class AppendCharSqStartEndMethod
- {
- public static void main(String args[])
- {
- StringBuffer str1 = new StringBuffer("Come ");
- StringBuffer str2 = new StringBuffer("1A2B");
- System.out.println("Output for append(CharSequence s, int start, int end):");
- System.out.println();
- System.out.println("Buffer is: " + str1);
- CharSequence CS1 = "back soon";
- str1.append(CS1, 3, 9);
- System.out.println("String str1 after append: " + str1);
- System.out.println("Buffer is: " + str2);
- CharSequence CS2 = "=B2A1+345";
- str2.append(CS2, 5, 8);
- System.out.println("String str2 after append: " + str2);
- }
- }
Output
Appending a new line to StringBuffer
Whenever we append content to the StringBuffer, the contents are appended to the end of the sequence without any spaces or line breaks between the contents.
Example
- public class AppendNewLine
- {
- public static void main(String args[])
- {
- StringBuffer str = new StringBuffer("Come");
- str.append("again");
- str.append("later");
- str.append("..!!");
- System.out.println("Output is: " + str);
- }
- }
Output
Now if we want to append spaces, then it would not be that much difficult to do that. Only we need to do is that: str.append(“”);
But if we want to append a new line, then it's a little bit difficult to do and there are many ways to do that. But most of them are platform-dependent, in other words, work for one platform like “\r\n” works for Windows but not for Unix (“\n” will work).
When implementing a StringBuffer, we have two platform-dependent ways to append a new line.
- Str.append(System.getProperty(“line.separator”));
- And
- Str.append(System.lineSeparator()); (works on jdk 1.7 or higher)
Let's see the code example for both ways.
Example
- public class AppendNewLineMethods
- {
- public static void main(String args[])
- {
- StringBuffer str1 = new StringBuffer("Have nice");
- System.out.println("Output for 1st way:");
- System.out.println();
- str1.append(System.getProperty("line.separator"));
- str1.append("time");
- System.out.println(str1);
- System.out.println();
- System.out.println("Output for 2nd way:");
- System.out.println();
- StringBuffer str2 = new StringBuffer("Good");
- str2.append(System.lineSeparator());
- str2.append("Luck");
- str2.append(System.lineSeparator());
- str2.append("Everyone");
- System.out.println(str2);
- }
- }
Output
Click below link to switch to next lecture.
thank you, keep learning and sharing.