Site icon

Convert given string to character array in java (with example)

1. Example: Convert String to Char array in java

  1. Given an input string in java.
    • e.g inputString =”Internet of things”
  2. We would like to convert input string to char array.
    • Output should be:
    • Character array is : ‘I’ ‘n’ ‘t’ ‘e’ ‘r’ ‘n’ ‘e’ ‘t’ ‘ ‘ ‘o’ ‘f’ ‘ ‘ ‘t’ ‘h’ ‘i’ ‘n’ ‘g’ ‘s’ .

2. Program – convert String to char array in java

package org.learn;

public class ConvertStringToArray {
    public static void main(String[] args) {
        String inputString = "Internet of things";

        System.out.println("1. Input String is = " + inputString);
        System.out.print("2. Character array is : ");

        char[] buffer = inputString.toCharArray();
        for (char ch : buffer) {
            System.out.printf("'%c' ", ch);
        }
    }
}

3. Output – convert String to char array in java

1. Input String is = Internet of things
2. Character array is : 'I' 'n' 't' 'e' 'r' 'n' 'e' 't' ' ' 'o' 'f' ' ' 't' 'h' 'i' 'n' 'g' 's' 
Exit mobile version