How to convert String to char in Java
Example:
Input: str="A"
Output: ch='A'
Approach: 1 : Using charAt()
Java
public class StringToChar {public static void main(String[] args) {String str ="A";char ch = stringToChar(str);System.out.println("char is " + ch);}private static char stringToChar(String ch) {return ch.charAt(0);}}
Approach : 2: Using toCharArray()
Java
public class StringToChar {public static void main(String[] args) {String str ="A";char ch = stringToChar(str);System.out.println("char is " + ch);}private static char stringToChar(String ch) {return ch.toCharArray()[0];}}
No comments:
Post a Comment