
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
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).