Mengubah String menjadi Array Karakter dan kemudian Mengubah array Karakter kembali ke 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: Mengonversi kembali array Karakter ke 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
// CARA Kedua: Mengonversi kembali array Karakter ke String
String.valueOf(characterArray);
// CARA Ketiga: Mengonversi kembali array Karakter ke String
Arrays.stream(characterArray)
.mapToObj(i -> (char)i)
.collect(Collectors.joining());
Mengonversi string ke Array Karakter
Character[] charObjectArray =
givenString.chars().
mapToObj(c -> (char)c).
toArray(Character[]::new);
Mengubah array karakter menjadi Array Karakter
String givenString = "MyNameIsArpan";
char[] givenchararray = givenString.toCharArray();
String.valueOf(givenchararray).chars().mapToObj(c ->
(char)c).toArray(Character[]::new);
Manfaat Mengkonversi Array karakter ke Array Karakter Anda dapat menggunakan funtion Arrays.stream untuk mendapatkan sub array
String subStringFromCharacterArray =
Arrays.stream(charObjectArray,2,6).
map(String::valueOf).
collect(Collectors.joining());