String to char in java

Java convert string to char using charAt() method

package com.w3spoint;
 
public class StringToChar {
  public static void main(String args[]){
      String str = "w3spoint";
      for(int i=0; i<str.length();i++){
    	//Retrieve char from string
        char ch = str.charAt(i);
        System.out.println("Character at "+i+" Position: "+ch);
      } 
  }
}

Output:

Character at 0 Position: w
Character at 1 Position: 3
Character at 2 Position: s
Character at 3 Position: p
Character at 4 Position: o
Character at 5 Position: i
Character at 6 Position: n
Character at 7 Position: t

Download this example.

Content Protection by DMCA.com
Please Share