convert string to string array code example

Example 1: how to convert string to array in java

How to convert string to array

import java.util.*; 
  
public class GFG { 
  
    public static void main(String args[]) 
    { 
  
        String str = "GeeksForGeeks"; 
  
        // Creating array and Storing the array 
        // returned by toCharArray() 
        char[] ch = str.toCharArray(); 
  
        // Printing array 
        for (char c : ch) { 
            System.out.println(c); 
        } 
    } 
}

Example 2: turn array into string

const elements = ['Fire', 'Air', 'Water'];

console.log(elements.join());
// expected output: "Fire,Air,Water"

console.log(elements.join(''));
// expected output: "FireAirWater"

console.log(elements.join('-'));
// expected output: "Fire-Air-Water"

Example 3: how to convert a sentence into string array in java

String str = "This is a simple sentence";
String[] strgs = str.split(" ");

Example 4: how to convert string into string array

public static void main(String[] args) {
    String str = "abcdef";

    String [] array = str.split(""); //   {"a", "b", "c", "d", "e", "f" }.
    
}

Tags:

Php Example