java

How to Convert String to Char in Java

In this tutorial, we are going to see how to convert string to char in Java. We can convert a string using the charAt() method of the String class.
 

Program to convert string to char :
public class StringToChar {
 
	public static void main(String[] args) 
	{
	   String strVar = "StackHowTo";
	      
	   int t = strVar.length();
	      
	   for(int i=0; i < t ; i++)
	   {
	      //charAt method finds the position and converts to char.
	      char charVar = strVar.charAt(i);
	        
	      System.out.println("The character at position "+i+" is: "+charVar);
	   }
 
	}
 
}
 
Output:

The character at position 0 is: S
The character at position 1 is: t
The character at position 2 is: a
The character at position 3 is: c
The character at position 4 is: k
The character at position 5 is: H
The character at position 6 is: o
The character at position 7 is: w
The character at position 8 is: T
The character at position 9 is: o
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *