Java - StringWriter append(CharSequence csq,int start,int end) method
Description
The Java StringWriter append(CharSequence csq,int start,int end) method appends a subsequence of the specified character sequence to this writer.
Declaration
Following is the declaration for java.io.StringWriter.append(CharSequence csq,int start,int end) method.
public StringWriter append(CharSequence csq,int start,int end)
Parameters
csq − The character sequence to append. If csq is null, then the four characters "null" are appended to this writer.
start − The index of the first character in the subsequence.
end − The index of the character following the last character in the subsequence.
Return Value
This method returns this writer.
Exception
IndexOutOfBoundsException − If start or end are negative, start is greater than end, or end is greater than csq.length().
Example - Usage of StringWriter append(CharSequence csq,int start,int end) method
The following example shows the usage of StringWriter append(CharSequence csq,int start,int end) method.
StringWriterDemo.java
package com.tutorialspoint;
import java.io.StringWriter;
public class StringWriterDemo {
public static void main(String[] args) {
// create a new writer
StringWriter sw = new StringWriter();
// create a new sequence
CharSequence sq = "Hello World";
// append sequence
sw.append(sq, 0, 5);
// print result
System.out.println("" + sw.toString());
// append second sequence
sw.append(sq, 5, 5);
// print result again
System.out.println("" + sw.toString());
}
}
Output
Let us compile and run the above program, this will produce the following result −
Hello Hello
Example - Appending a Substring from a String
The following example shows the usage of StringWriter append(CharSequence csq,int start,int end) method.
StringWriterDemo.java
package com.tutorialspoint;
import java.io.StringWriter;
public class StringWriterDemo {
public static void main(String[] args) throws Exception {
StringWriter sw = new StringWriter();
String text = "HelloWorld";
sw.append(text, 0, 5); // "Hello"
sw.append(" ");
sw.append(text, 5, 10); // "World"
System.out.println("Output: " + sw.toString());
}
}
Output
Let us compile and run the above program, this will produce the following result−
Output: Hello World
Explanation
Appends "Hello" from index 0 to 5.
Appends "World" from index 5 to 10.
Example - Appending a Portion of StringBuilder
The following example shows the usage of StringWriter append(CharSequence csq,int start,int end) method.
StringWriterDemo.java
package com.tutorialspoint;
import java.io.StringWriter;
public class StringWriterDemo {
public static void main(String[] args) {
StringWriter sw = new StringWriter();
StringBuilder sb = new StringBuilder("FunctionalInterfaces");
sw.append(sb, 0, 10); // "Functional"
sw.append(" ");
sw.append(sb, 10, sb.length()); // "Interfaces"
System.out.println("Output: " + sw.toString());
}
}
Output
Let us compile and run the above program, this will produce the following result−
Output: Functional Interfaces
Explanation
StringBuilder content is split using indices.
Appends "Functional" (0 to 10) and "Interfaces" (10 to end).