Tutorialspoint examples

Learn java tutorial with examples for beginners online

Convert string to char java program


Java convert string to char using charAt() method

package com.tutorialspointexamples;
 
public class StringToChar {
  public static void main(String args[]){
      String str = "tutorialspointexamples";
      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: t
Character at 1 Position: u
Character at 2 Position: t
Character at 3 Position: o
Character at 4 Position: r
Character at 5 Position: i
Character at 6 Position: a
Character at 7 Position: l
Character at 8 Position: s
Character at 9 Position: p
Character at 10 Position: o
Character at 11 Position: i
Character at 12 Position: n
Character at 13 Position: t
Character at 14 Position: e
Character at 15 Position: x
Character at 16 Position: a
Character at 17 Position: m
Character at 18 Position: p
Character at 19 Position: l
Character at 20 Position: e
Character at 21 Position: s

Download this example.

Download tutorialspointexamples Android App
Copyright © 2018 Tutorialspoint examples DMCA.com Protection Status