Converting String to "Character" array in Java
I want to convert a String
to an array of objects of Character class but I am unable to perform the conversion. I know that I can convert a String to an array of primitive datatype type "char" with the toCharArray()
method but it doesn't help in converting a String to an array of objects of Character type.
How would I go about doing so?
Use this:
String str = "testString";
char[] charArray = str.toCharArray();
Character[] charObjectArray = ArrayUtils.toObject(charArray);
One liner with java-8:
String str = "testString";
//[t, e, s, t, S, t, r, i, n, g]
Character[] charObjectArray =
str.chars().mapToObj(c -> (char)c).toArray(Character[]::new);
What it does is:
- get an
IntStream
of the characters (you may want to also look atcodePoints()
) - map each 'character' value to
Character
(you need to cast to actually say that its really achar
, and then Java will box it automatically toCharacter
) - get the resulting array by calling
toArray()
Why not write a little method yourself
public Character[] toCharacterArray( String s ) {
if ( s == null ) {
return null;
}
int len = s.length();
Character[] array = new Character[len];
for (int i = 0; i < len ; i++) {
/*
Character(char) is deprecated since Java SE 9 & JDK 9
Link: https://docs.oracle.com/javase/9/docs/api/java/lang/Character.html
array[i] = new Character(s.charAt(i));
*/
array[i] = s.charAt(i);
}
return array;
}
Converting String to Character Array and then Converting Character array back to String
//Givent String
String given = "asdcbsdcagfsdbgdfanfghbsfdab";
//Converting String to Character Array(It's an inbuild method of a String)
char[] characterArray = given.toCharArray();
//returns = [a, s, d, c, b, s, d, c, a, g, f, s, d, b, g, d, f, a, n, f, g, h, b, s, f, d, a, b]
//ONE WAY : Converting back Character array to String
int length = Arrays.toString(characterArray).replaceAll("[, ]","").length();
//First Way to get the string back
Arrays.toString(characterArray).replaceAll("[, ]","").substring(1,length-1)
//returns asdcbsdcagfsdbgdfanfghbsfdab
or
// Second way to get the string back
Arrays.toString(characterArray).replaceAll("[, ]","").replace("[","").replace("]",""))
//returns asdcbsdcagfsdbgdfanfghbsfdab
//Second WAY : Converting back Character array to String
String.valueOf(characterArray);
//Third WAY : Converting back Character array to String
Arrays.stream(characterArray)
.mapToObj(i -> (char)i)
.collect(Collectors.joining());
Converting string to Character Array
Character[] charObjectArray =
givenString.chars().
mapToObj(c -> (char)c).
toArray(Character[]::new);
Converting char array to Character Array
String givenString = "MyNameIsArpan";
char[] givenchararray = givenString.toCharArray();
String.valueOf(givenchararray).chars().mapToObj(c ->
(char)c).toArray(Character[]::new);
benefits of Converting char Array to Character Array you can use the Arrays.stream funtion to get the sub array
String subStringFromCharacterArray =
Arrays.stream(charObjectArray,2,6).
map(String::valueOf).
collect(Collectors.joining());