Converting String to "Character" Array in Java

Converting String to Character array in Java

Use this:

String str = "testString";
char[] charArray = str.toCharArray();
Character[] charObjectArray = ArrayUtils.toObject(charArray);

What is the need of convert a String to charArray?

Converting from String to char[] is useful if you want to do something with the order of the elements, for example sort() them.

String is immutable and not very suited for manipulation.

For example:

    String original = "bharti";
char[] chars = original.toCharArray();
Arrays.sort(chars);
String sorted = new String(chars);
System.out.println(sorted);

which prints:

abhirt


Also, some methods/classes explicitly require a char[] as input, for example PBEKeySpec

byte[] salt = new byte[16];
random.nextBytes(salt);
KeySpec spec = new PBEKeySpec("password".toCharArray(), salt, 65536, 128);
SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] hash = f.generateSecret(spec).getEncoded(…

The rationale is that you can wipe the char[] contents from memory. See more information here: https://stackoverflow.com/a/8881376/461499

How to convert a char array back to a string?

No, that solution is absolutely correct and very minimal.

Note however, that this is a very unusual situation: Because String is handled specially in Java, even "foo" is actually a String. So the need for splitting a String into individual chars and join them back is not required in normal code.

Compare this to C/C++ where "foo" you have a bundle of chars terminated by a zero byte on one side and string on the other side and many conversions between them due do legacy methods.

Converting String Array list to char array

You are getting a null pointer exception because you initialized the char array to null. Instead, you would want to initialize it to a new char array size equal to that of the ArrayList like:

char[] list = new char[code.size()];

Here is how all the code would look.

List<Character> code = new ArrayList<Character>();
code.add("A");
code.add("B");
code.add("C");

char[] list = new char[code.size()]; // Updated code

for(int i=0; i<code.size(); i++){
list[i] = code.get(i);
System.out.println(list[i]);
}

Convert element in string array to character

you can convert string to char array

char[] ch= stringtest[3].toCharArray();
if(ch[0]=='m')

or you can use charAt method

if(stringtest[3].charAt(0)=='m')

Converting string to char array in Java?

You need to have a double array. Something like

char[][] arr = new char[2][];
arr[0] = a.toCharArray();

Converting String to Char array in JAVA

nArray[i] is a char; so is the '\n' constant. Character is an unsigned integral type, so characters are added together in the same way as all integers - numerically. When an addition happens, you end up with an int, not a char, so calling println on it produces a numeric result.

Removing + '\n' will fix the problem. You would get a newline character from println, so all characters would appear on a new line.

Demo.

How can I convert a string to char[] without copying the object?

No, it isn't. Not without reflection, which you should avoid. Messing with the underlying char[] in a string via reflection is a recipe for subtle bugs. You can access individual characters in a string using charAt, but if you really need a char[] you should just call toCharArray.

If this is not possible, is there a reason so?

First reason: encapsulation. The array is a private implementation detail.

Second reason: immutability. Strings are immutable, but arrays never are. So you could modify the underlying char array, and the string would be mutated, much to the surprise of any developer relying on the normal immutability.



Related Topics



Leave a reply



Submit