I want to convert a String to Character array but I couldn't make it. I can convert it to char array with toCharArray() method but it doesn't be the same with Character array.

share|improve this question
5  
What do you mean by "doesn't fit with the character array"? – barsju Apr 4 '12 at 6:47
Can you re-word this or articulate a bit more, or perhaps provide a code example? – blackcompe Apr 4 '12 at 6:50
To convert char to Character, use Character.valueOf(mychar). If it is an array, loop each element and convert. – ee. Apr 4 '12 at 6:50
feedback

6 Answers

up vote 14 down vote accepted

Use this String.toCharArray() and then use ArrayUtils.toObject(char[])

String str = "testString";
char[] charArray = str.toCharArray();
Character[] charObjectArray = ArrayUtils.toObject(charArray);
share|improve this answer
3  
I wish ArrayUtils is standard in JVMs! – Alvin Apr 4 '12 at 6:58
feedback

You have to write your own method in this case. Use a loop and get each character using charAt(i) and set it to your Character[] array using arrayname[i] = string.charAt[i].

share|improve this answer
feedback

Why not write a little method yourself?

public Character[] toCharacterArray(String s) {
   if (s == null) {
     return null;
   }
   Character[] array = new Characer[s.length()];
   for (int i = 0; i < s.length(); i++) {
      array[i] = new Character(s.charAt(i));
   }

   return array;
}
share|improve this answer
+1 for Character[] array = new Characer[s.length()]; – Ankit Oct 21 '12 at 10:33
feedback

String#toCharArray returns an array of char, what you have is an array of Character. In most cases it doesn't matter if you use char or Character as there is autoboxing. The problem in your case is that arrays are not autoboxed, I suggest you use an array of char (char[]).

share|improve this answer
feedback

another way to do it.

String str="I am a good boy";
    char[] chars=str.toCharArray();

    Character[] characters=new Character[chars.length];
    for (int i = 0; i < chars.length; i++) {
        characters[i]=chars[i];
        System.out.println(chars[i]);
    }
share|improve this answer
feedback

I hope the code below will help you.

String s="Welcome to Java Programming";
char arr[]=s.toCharArray();
for(int i=0;i<arr.length;i++){
    System.out.println("Data at ["+i+"]="+arr[i]);
}

It's working and the output is:

Data at [0]=W
Data at [1]=e
Data at [2]=l
Data at [3]=c
Data at [4]=o
Data at [5]=m
Data at [6]=e
Data at [7]= 
Data at [8]=t
Data at [9]=o
Data at [10]= 
Data at [11]=J
Data at [12]=a
Data at [13]=v
Data at [14]=a
Data at [15]= 
Data at [16]=P
Data at [17]=r
Data at [18]=o
Data at [19]=g
Data at [20]=r
Data at [21]=a
Data at [22]=m
Data at [23]=m
Data at [24]=i
Data at [25]=n
Data at [26]=g
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.