append(CharSequence, int, int): This method is available in the java.io.StringWriter class of Java.
Syntax:
StringWriter java.io.StringWriter.append(CharSequence csq, int start, int end)
This method takes three arguments. This method appends a subsequence of the specified character sequence to this writer.
Parameters: Three parameters are required for this method.
csq: The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".
start: The index of the first character in the subsequence.
end: The index of the character following the last character in the subsequence.
Returns: This writer.
Throws:
1. IndexOutOfBoundsException - If the start or end is negative, the start is greater than the end, or the end is greater than csq.length().
Approach 1: When no exception
Java
import java.io.StringWriter;public class StringWriterappend3 {public static void main(String[] args) {StringWriter stringWriter = new StringWriter();CharSequence csq = new String("HELLO JAVA");int start = 0, end = 4;stringWriter.append(csq, start, end);System.out.println("Successfully appended");}}
Output:
Successfully appended
Approach 2: IndexOutOfBoundsException
Java
package com.StringWriter;import java.io.StringWriter;public class StringWriterappend3 {public static void main(String[] args) {StringWriter stringWriter = new StringWriter();CharSequence csq = new String("HELLO JAVA");int start = 0, end = 14;stringWriter.append(csq, start, end);System.out.println("Successfully appended");}}
Output:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end 14, length 10 at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3734) at java.base/java.lang.String.substring(String.java:1903) at java.base/java.lang.String.subSequence(String.java:1942) at java.base/java.io.StringWriter.append(StringWriter.java:190) at com.StringWriter.StringWriterappend3.main(StringWriterappend3.java:12)
Some other methods of StringWriter
append(char): This method appends the specified character to this writer.
append(CharSequence): This method appends the specified character sequence to this writer.
append(CharSequence, int, int): This method appends a subsequence of the specified character sequence to this writer.
close(): This method closes a StringWriter.
flush(): This method flushes the stream.
getBuffer(): This method returns the string buffer itself.
StringWriter(): This method creates a new string writer using the default initial string buffer size.
StringWriter(int): This method creates a new string writer using the specified initial string buffer size.
toString(): This method returns the buffer's current value as a string.
write(int): This method writes a single character.
write(String): This method writes a string.
write(char[], int, int): This method writes a portion of an array of characters.
write(String, int, int): This method writes a portion of a string.
No comments:
Post a Comment