How to convert/parse from String to char in java? How to convert/parse from String to char in java? java java

How to convert/parse from String to char in java?


If your string contains exactly one character the simplest way to convert it to a character is probably to call the charAt method:

char c = s.charAt(0);


You can use the .charAt(int) function with Strings to retrieve the char value at any index. If you want to convert the String to a char array, try calling .toCharArray() on the String.

String g = "line";char c = g.charAt(0);  // returns 'l'char[] c_arr = g.toCharArray(); // returns a length 4 char array ['l','i','n','e']


you can use this trick :

String s = "p";char c = s.charAt(0);