Languages
[Edit]
EN

Java - convert String to array

7 points
Created by:
TheDalot
390

In Java, we can convert String to array in following ways.

1. Using String.split()

package com.dirask.examples;

import java.util.Arrays;

public class Example1 {

    public static void main(String[] args) {

        String str = "abc";
        String[] array = str.split("");

        System.out.println(Arrays.toString(array)); // [a, b, c]
    }
}

Output: 

[a, b, c]

2. Using iteration and String.valueOf()

package com.dirask.examples;

import java.util.Arrays;

public class Example2 {

    public static void main(String[] args) {

        String str = "abc";
        String[] array = new String[str.length()];

        for (int i = 0; i < str.length(); i++) {
            array[i] = String.valueOf(str.charAt(i));
        }

        System.out.println(Arrays.toString(array)); // [a, b, c]
    }
}

Output:

[a, b, c]

 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - String conversion

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join