Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conversion of string into char

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
May I know how to do the above conversion?

Rgds,
Janet
 
YOu can't convert a string into a char because this isn't rational. A string is often MANY chars. Which one do you want to become your char? The first? The last?

What you can do is convert a string into an array of chars or get a char at a specific location that you specify. For instance, if you are reading user input and want to find out if the user entered a 'y' or a 'n' you can do:

String input = reader.readLine();

if (input!= null && input.charAt(0) == 'y')
// user entered a 'y'
else
// user entered something else

Alternately you can get the chars that make up this string:

char [] chars = myString.toCharArray();

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top