Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you think you have some skills to help others

help someone and become a part of our community - List Q & A

Java - convert String to array

0 contributions
7 points

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]

 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more