1

Is there a way to convert a String variable of the type "X" to a character ?

String state = "X";
char c_state = convertToChar(state);

How do I do this ?

3 Answers 3

5

You could do:

char c_state = state.charAt(0);
Sign up to request clarification or add additional context in comments.

1 Comment

I laughed out loud at this ! Thanks
0

You could also convert it into a char array as follows, which could be quite useful if the String contained more than 1 character.

char[] charArray = state.toCharArray();

1 Comment

That creates a copy of the array, which might turn out to be slightly less efficient. (I don't know how well charAt gets optimised.)
0

This is another approach

char c_state = state.toCharArray()[0];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.