aspose file tools
The moose likes Beginning Java and the fly likes how to convert from string( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to convert from string("a") to char.." Watch "how to convert from string("a") to char.." New topic

how to convert from string("a") to char..

Eung maeng
Ranch Hand

Joined: Feb 10, 2002
Posts: 68
Does anyone explain to me how to convert from string("Y") to char?

John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
String y = "Y";
char y_char = y.charAt(0);

And if the string is bigger than 1 letter you can do the same thing with a loop or break up the string and send it into a character array with the following:

String y = "hello";
char[] array = y.toCharArray();

array = {'h', 'e', 'l', 'l', 'o'}
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

I plan to use the following method to convert String to char:

This code doesn't throw an exception, but it doesn't print the null characters either.

Is this OK?

It is sorta covered in the JavaRanch Style Guide.
subject: how to convert from string("a") to char..