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:
1 2 3 4 5 6 7 8 9 10 |
class Check { public static void main(String arg[]) { char ca[]={'h','i','m','m','u'}; String s=new String(ca); System.out.println("formed string :"+s); System.out.println("its length :"+s.length()); } } |
Output:
1 2 |
formed string :himmu its length :5 |
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:
1 2 3 4 5 6 |
char ca[]={'h','i','m','a','n','i'}; String s=""; for(int i=0;i<ca.length;i++) s+=ca[i]; System.out.println("formed string :"+s); System.out.println("its length :"+s.length()); |
Output:
1 2 |
formed string :himani its length :6 |
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:
1 2 3 4 5 6 7 |
char ca[]={'g','a','n','d','h','i'}; StringBuffer sb=new StringBuffer("lengthytext"); for(int i=0;i<ca.length;i++) sb.setCharAt(i,ca[i]); sb=new StringBuffer(sb.substring(0,ca.length)); System.out.println("formed string :"+sb); System.out.println("its length :"+sb.length()); |
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
1 2 |
char ca[]={'d','e','v','a','k','i'}; String s=String.copyValueOf(ca); |
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:
1 2 |
char ca[]={'g','o','w','t','h','a','m'}; String s=String.valueOf(ca,2,5); |
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
1 2 |
char ca[]={'g','o','w','t','h','a','m'}; String s=String.valueOf(ca); |
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.