Signup/Sign In

CharArrayWriter append() method in Java

In this tutorial, we will learn about append() method from CharArrayWriter class in Java. This method comes in three different overloading methods to provide better ways of appending. This method takes data to be appended on CharArrayWriter and returns CharArrayWriter after appending the character into it.

Syntax

This overloading method of an append method will take a character as a parameter to append a CharArrayWriter. This method returns CharArrayWriter after appending the character into it.

public CharArrayWriter append(char c)

This overloading method of an append method will take a sequence of characters as an input to append a CharArrayWriter. This method returns CharArrayWriter after appending the character into it.

public CharArrayWriter append(CharSequence sequence)

This overloading method of an append method will take a sequence of characters as an input to append a CharArrayWriter with that it also takes two parameters to start as a starting index and end as an ending index to slice the given sequence of characters. This method returns CharArrayWriter after appending the character into it.

public CharArrayWriter append(CharSequence sequence, int start, int end )  

Example of the append() Method

This is one of the overloading methods of append() method it will receive the only character as an input and appends to it and then returns CharArrayWriter after appending the character into it.

import java.io.CharArrayWriter;
public class StudyTonight 
{
	public static void main(String[] args) 
	{  
		CharArrayWriter charArrayWriter = new CharArrayWriter();  
		charArrayWriter.append('A');
		charArrayWriter.append('B');
		charArrayWriter.append('C');
		System.out.println(charArrayWriter.toString());  
	}  
}


ABC

Example of the append() Method

This method will receive a string and append it to the CharacterWriter. Once the string gets appended to the CharacterWriter it will return it. In the code given below, we called this method twice by passing the string and after the toString method, we are retrieved it and we can see it has appended those strings.

import java.io.CharArrayWriter;
public class StudyTonight 
{
	public static void main(String[] args) 
	{  
		CharArrayWriter charArrayWriter = new CharArrayWriter();  
		charArrayWriter.append("Hello ");
		charArrayWriter.append("Studytonight");
		System.out.println(charArrayWriter.toString());  
	}  
}


Hello Studytonight

Example of the append() Method

This example is very much similar to the above example the only difference is while passing the CharSequence as a parameter to the append method we also pass start and end parameters and these parameters will ensure that the give CharSequence is sliced according to it. and then returns CharArrayWriter after appending the character into it.

import java.io.CharArrayWriter;
public class StudyTonight 
{
	public static void main(String[] args) 
	{  
		CharArrayWriter charArrayWriter = new CharArrayWriter();  
		CharSequence charSequence="Welcome to Studytonight";
		charArrayWriter.append(charSequence, 3, 11);
		System.out.println(charArrayWriter.toString());  
	}  
}


come to

mailConclusion:

In this tutorial, we learned about the append() method This method comes in three different overloading methods to provide better ways of appending. This method takes data to be appended on CharArrayWriter and returns CharArrayWriter after appending the character into it.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.