Java.io.StringWriter.append() Method



Description

The java.io.StringWriter.append() method appends a subsequence of the specified character sequence to this writer.

Declaration

Following is the declaration for java.io.StringWriter.append() 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

The following example shows the usage of java.io.StringWriter.append() method.

package com.tutorialspoint;

import java.io.*;

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, 6, 5);

      // print result again
      System.out.println("" + sw.toString());
   }
}

Let us compile and run the above program, this will produce the following result −

Hello
Hello World
java_io_stringwriter.htm
Advertisements