Latest :

Copying Character Array To String In Java – Tutorial

Java tutorial to copy character array to string. Here, we will discuss about the various methods of copying a character array to a string using. If you have any doubts leave a comment here at the end of the post.

  • Creating a string object based on the character array.
  • Adding character by character to an initially empty string.
  • Setting character by character into a string buffer.
  • Using the static method CopyValueOf() of string class.
  • Using the static method ValueOf() of string class.

Creating a String object based on the character array

This is the simplest way of copying a character array into a String.

For Example:


In the above example, we have an array of 5 characters (containing the letters h, i, m, m and u). Length of this array is 5 (as we have 5 characters).

Then we have created a String object by specifying the character array as argument to constructor. With this a new String is created with the characters of the character array. It means the new String will be of length 5 with the characters h,i,m,m and u at 0,1,2,3 and 4 locations respectively. The output confirms it.

 Adding character by character to an (initially) empty string

Sample logic:


In this example, we have an empty string initially. And then, taken a loop that is repeated for N number of times where N is the number of characters in the array.

Each time (with each iteration i.e. from 0th position to N-1th position) a character is taken from the array and appended to the String. Then we have printed the string and its length. The resulting string will be “himani” and its length 6.

Setting character by character into a String Buffer

Sample Logic:

We cannot change the content of an existing String object. But we can change the content of a StringBuffer or StringBuilder object.

In the above example, we have taken a StringBuffer object that already contains some content. With the existing content, the length of the StringBuffer object is 11.

Then we have taken each character from the character array and stored in the locations 0,1,2,etc… it means, the letter ‘g’ (from 0th location of the array) replaces ‘l’ (the 0th letter in StringBuffer object) and ‘a’ replaces ‘e’, ‘n’ replaces ‘n’, ‘d’ replaces ‘g’, ‘h’ replaces ‘t’ and ‘i’ replaces ‘h’.

By the end of the for loop the replacements will be complete and the StringBuffer object contains “gandhiytext”. Then we have taken a substring of the StringBuffer object.

We have taken the first 6 letters of the StringBuffer object and left the remaining letters. So, by the time we go for printing, we will have “gandhi” in the StringBuffer object and its length will be 6.

Using the static method copyValueOf() of String class

This is a simple static method in String class that converts a character array data into a String object.

If we want only part of the data from the character array to be copied (created) to the String, then we can use an overloaded version of copyValueOf() that takes the character array and two integers as arguments.

The first integer indicates the location from where to copy and second integer indicates the number of character to consider.

For Example:

In the above example, we have 3 arguments to valueOf(). The first one is character array. Out of the two integers, first one (2) is the starting location and the second one (5) is the number of characters to consider. With the above example, the new String will contain “wtham”. Similarly the following example creates a new string “wth”.(it means the 3 characters from 2nd location)

Using the static method valueOf() of String class

This is also similar to copyValueOf(). This method also has overloaded versions like valueOf(char[],int,int) and valueOf(Object). Both of them can serve the purpose.


Check Also

What is Recursion In Java Programming – JavaTutoring

What is Recursion In Java programming – Here we cover in-depth article to know more ...