StringBuilder append() Method in Java (original) (raw)
Last Updated : 15 Dec, 2024
In Java, the **append() method of StringBuilder class is used to add data to the end of an existing StringBuilder object. It supports appending different data types like **strings, **integers, characters, and **booleans by making it easy for dynamic string manipulation.
**Example 1: Here, we use the **append() method **to add a **string to a StringBuilder object.
Java `
// append() with strings public class Geeks { public static void main(String[] args) {
// Create a StringBuilder object with
// an initial string
StringBuilder sb = new StringBuilder("Java");
// Append a string to the StringBuilder
sb.append(" Programming");
System.out.println(sb);
}
}
`
**Now there are several versions of the append() method in StringBuilder and each version designed to append different types of data, such as strings, different data types, objects, or even subsequences.
Syntax of append() Method
public StringBuilder append(data)
- **Parameter: data: The value to append (can be any data type).
- **Return Type: StringBuilder: It returns the updated StringBuilder object.
**Example 2: In this example, we use the append() method **to add various **data types to the StringBuilder object.
Java `
// append() with multiple data types public class Geeks { public static void main(String[] args) {
// Create a StringBuilder object
StringBuilder sb = new StringBuilder("The value is ");
// Append various data types
sb.append(21);
sb.append(", ");
sb.append(7.01);
sb.append(", or ");
sb.append(false);
System.out.println(sb);
}
}
`
Output
The value is 21, 7.01, or false
**Example 3: StringBuilder append(char[] str, int offset, int len)
This version appends a **subarray of characters to the StringBuilder, starting at the specified offset and with the specified length.
**Syntax:
public StringBuilder append(char[] str, int offset, int len)
**Parameter:
- **char[] str: The character array that contains the characters to be appended.
- **int offset: The index in the character array where the subarray starts.
- **int len: The number of characters to append from the array starting from the offset.
**Return Type: StringBuilder: The method returns the same StringBuilder instance with the appended characters.
Java `
// append() with character arrays public class Geeks { public static void main(String[] args) {
// Create a StringBuilder object
StringBuilder sb = new StringBuilder("Programming ");
// Create a character array
char[] ch = {'i', 'n', ' ', 'J', 'a', 'v', 'a'};
// Append part of the character array
sb.append(ch, 0, 4); // Appends "in J"
System.out.println(sb);
}
}
`
**Example 4: StringBuilder append(CharSequence chseq, int start, int end)
This version appends a subsequence of characters from a **CharSequence to the StringBuilder by using specified start and end indices.
**Syntax:
public StringBuilder append(CharSequence chseq, int start, int end)
**Parameter:
- **CharSequence chseq: The CharSequence like String, StringBuilder, etc. from which the subsequence is appended.
- **int start: The starting index of the subsequence.
- **int end: The ending index of the subsequence (exclusive).
**Return Type:
- **StringBuilder: The method returns the same StringBuilder instance with the appended subsequence. Java `
// append() with CharSequence public class Geeks { public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Welcome to ");
CharSequence ch = "JavaProgramming";
// Appends 'Programming' from the CharSequence
sb.append(ch, 5, 14);
System.out.println(sb);
}
}
`
Output
Welcome to rogrammin
**Example 5: StringBuilder append(Object obj)
This version appends the string representation of an object to the StringBuilder.
**Syntax:
public StringBuilder append(Object obj)
- **Parameter: Object obj: The object whose string representation will be appended to the StringBuilder.
- **Return Type: StringBuilder: The method returns the same StringBuilder instance with the appended string representation of the object. Java `
// append() with Object public class GFG {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("");
Object o = 21;
// Appends the string representation
// of the object
sb.append(o);
System.out.println(sb);
}
}
`
**Example 6: This example demonstrates chaining multiple append() calls.
Java `
// chaining append() calls public class Geeks { public static void main(String[] args) {
// Create a StringBuilder object
StringBuilder sb = new StringBuilder();
// Chain multiple append() calls
sb.append("Java").append(" is a ").append("Programming Language.");
System.out.println(sb);
}
}
`
Output
Java is a Programming Language.