public static String char2Str(char c) {
return Integer.toString(c);
}
EDIT:
For example, char2Str('ɮ')
returns 622
.
How do I implement the opposite method of the above (str2Char()
)?
public static String char2Str(char c) {
return Integer.toString(c);
}
EDIT:
For example, char2Str('ɮ')
returns 622
.
How do I implement the opposite method of the above (str2Char()
)?
You should have something like that:
Char to String
public static String char2Str(char c) {
return Character.toString(c);
}
String to Char
When converting String to char, your output will be an array of characters as bellow:
public static char[] str2Char(String s) {
return s.toCharArray();
}
If you know String is of single length (just a single char), Use
char c = s.charAt(0);
Your method
public static char str2Char(String s) {
return s.charAt(0);
}
you have to be sure that your string contains only 1 char, then use
s.charAt(0)
if not, you can get the array of chars and then return the one you need or the whole array
char[] arr = s.toCharArray();
Well you can find the answer right here : https://stackoverflow.com/a/7853536/9623806
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);