Java toCharArray() method

    toCharArray() method

    This method will allow you to convert the provided string into the array of the character. A new character array will be created and the length will be the same as the string.

    Method implementation-

    public char[] toCharArray() {  
    
          // Cannot use Arrays.copyOf because of class initialization order issues  
          char result[] = new char[value.length];  
          System.arraycopy(value, 0, result, 0, value.length);  
          return result;  
      }  

    Syntax-

    public char[] toCharArray()  

    Example-

    public class Simple{  
       public static void main(String[] args) {      
          String s1="welcome";  
          char[] ch=s1.toCharArray();  
          for(int i=0;i<ch.length;i++){  
             System.out.print(ch[i]);  
           }  
        }
    }  

    Output-

    Java ToChar method