Java - CharArrayWriter append(CharSequence csq, int start, int end) method
Description
The Java CharArrayWriter append(CharSequence csq, int start, int end) method appends a portion of the specified character sequence to this writer.
Declaration
Following is the declaration for java.io.CharArrayWriter.append(CharSequence csq, int start, int end) method −
public CharArrayWriter append(CharSequence csq, int start, int end)
Parameters
csq − The character sequence to be appended. If the character sequence is null, the writer is appended with 4 characters 'null'.
start − The index of the first character in the portion
end − The index of the character following the end of the subsequence
Return Value
This CharArrayWriter
Exception
IndexOutOfBoundsException − If end is greater than sequence's length, start is greater than end, or start or end are negative.
NullPointerException − If csq is null.
Example - Using CharArrayWriter append(CharSequence csq, int start, int end) method
The following example shows the usage of Java CharArrayWriter append(CharSequence csq, int start, int end) method.
CharArrayWriterDemo.java
package com.tutorialspoint;
import java.io.CharArrayWriter;
public class CharArrayWriterDemo {
public static void main(String[] args) {
CharArrayWriter chw = null;
try {
// create character array writer
chw = new CharArrayWriter();
// declare character sequence
CharSequence csq = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// append character sequence to the writer
chw.append(csq, 2, 5);
// prints out the character sequences
System.out.println(chw.toString());
} catch(Exception e) {
// for any error
e.printStackTrace();
} finally {
// releases all system resources from writer
if(chw!=null)
chw.close();
}
}
}
Output
Let us compile and run the above program, this will produce the following result −
CDE
Example - Appending a Substring from a String
The following example shows the usage of Java CharArrayWriter append(CharSequence csq, int start, int end) method.
CharArrayWriterDemo.java
package com.tutorialspoint;
import java.io.CharArrayWriter;
public class CharArrayWriterDemo {
public static void main(String[] args) {
CharArrayWriter writer = new CharArrayWriter();
// Define a CharSequence (String)
String text = "Hello, Java World!";
// Append a substring from index 7 to 11 ("Java")
writer.append(text, 7, 11);
// Print the output
System.out.println(writer.toString()); // Output: Java
}
}
Output
Let us compile and run the above program, this will produce the following result −
Java
Explanation
A CharArrayWriter instance is created.
A String "Hello, Java World!" is defined.
The append(CharSequence csq, int start, int end) method appends characters from index 7 to 11 (substring "Java").
The result ("Java") is printed.
Example - Appending a Subsequence from a StringBuilder
The following example shows the usage of Java CharArrayWriter append(CharSequence csq, int start, int end) method.
CharArrayWriterDemo.java
package com.tutorialspoint;
import java.io.CharArrayWriter;
public class CharArrayWriterDemo {
public static void main(String[] args) {
CharArrayWriter writer = new CharArrayWriter();
// Create a StringBuilder
StringBuilder sb = new StringBuilder("Spring Boot Framework");
// Append a substring from index 7 to 11 ("Boot")
writer.append(sb, 7, 11);
// Append another substring from index 12 to 21 ("Framework")
writer.append(sb, 12, 21);
// Print the output
System.out.println(writer.toString()); // Output: BootFramework
}
}
Output
Let us compile and run the above program, this will produce the following result −
BootFramework
Explanation
A StringBuilder "Spring Boot Framework" is created.
The append method extracts "Boot" (index 7 to 11) and appends it.
Another call extracts "Framework" (index 12 to 21) and appends it.
The final output is "BootFramework".
Key Takeaways
Allows appending a specific range of characters from a CharSequence.
Supports String, StringBuilder, and StringBuffer.
Helps extract parts of text efficiently without extra substring operations.