ClaraBee
ClaraBee

Reputation: 13

How to split a string array into separate arrays in Java

I am trying to split my array into separate arrays.

I have a string of of words. I split the words into an array. Now I am trying to split the array of words into their own separate array.

Example:

string = "This is a string";
words = [This, is, a, string]

I would like my output to be:

[This], [is], [a], [string]

Here's what I have so far:

String[] words = string.split(" ");    
String wordsArray = Arrays.toString(words); 
System.out.println(wordsArray.split(" ").toString());

and this is my output:

[Ljava.lang.String;@63947c6b

Upvotes: 1

Views: 10138

Answers (6)

Sourabh Bhat
Sourabh Bhat

Reputation: 1893

If you want to simply print it then, it is very simple to do, using the streams api. Here is an example:

String output = Arrays.stream("This is a string".split("\\s"))
                      .map(s -> "[" + s + "]")
                      .collect(Collectors.joining(", "));

Upvotes: 0

Asim Zaka
Asim Zaka

Reputation: 556

It can simply done as:

String string = "This is a string";     
    System.out.println(string);     
    String[] words = string.split(" ");
    System.out.println(Arrays.toString(words));

    String[][] wordsArray= new String[words.length][1];
    for(int i=0;i<words.length;i++){
        wordsArray[i][0]=words[i]; 
    }
    System.out.println(Arrays.deepToString(wordsArray));

Upvotes: 0

Andreas
Andreas

Reputation: 159086

Do you mean like this, using Arrays.deepToString() to print nested arrays?

String string = "This is a string";
System.out.println(string);

String[] words = string.split(" ");
System.out.println(Arrays.toString(words));

String[][] wordsArray = new String[words.length][];
for (int i = 0; i < words.length; i++)
    wordsArray[i] = new String[] { words[i] };
System.out.println(Arrays.deepToString(wordsArray));

Output

This is a string
[This, is, a, string]
[[This], [is], [a], [string]]

If you just want to build the string you listed, this is however an easier way:

String string = "This is a string";
StringJoiner joiner = new StringJoiner("], [", "[", "]");
for (String word : string.split(" "))
    joiner.add(word);
System.out.println(joiner.toString());

Output

[This], [is], [a], [string]

Or same in a single statement using streams:

System.out.println(Pattern.compile(" ")
                          .splitAsStream("This is a string")
                          .collect(Collectors.joining("], [", "[", "]")));

Or you could just cheat and do this:

String string = "This is a string";
System.out.println("[" + string.replaceAll(" ", "], [") + "]");

Upvotes: 3

Gurwinder Singh
Gurwinder Singh

Reputation: 39467

String s = "THis is an example";
String[] sa = s.split("\\s+");
StringJoiner sj = new StringJoiner("], [");
for (String item : sa)
    sj.add(item);
System.out.println("[" + sj + "]");

Produces:

[THis], [is], [an], [example]

Upvotes: 0

Patrick Parker
Patrick Parker

Reputation: 4959

Here is one way you could build the String[][] :

public static void main(String[] args) {
    String str = "This is a string";
    String[][] result = Arrays.stream(str.split(" "))
        .map(word -> new String[] {word})
        .toArray(String[][]::new);
    // [[This], [is], [a], [string]]
    String output = Arrays.deepToString(result);
    output = output.substring(1, output.length()-1);
    System.out.println(output);
}

As you noticed, one does not simply pass an array to println, because its default toString method will not show its elements. Therefore, use the Arrays.toString or Arrays.deepToString methods to print an arrays elements.

Upvotes: 1

A Sdi
A Sdi

Reputation: 665

you can do it this way

words = "This is a string";
String[] wordsArray=words.split(" ");
Arrays.stream(wordsArray).map(s -> s="["+s+"]").forEach(System.out::println);

Upvotes: 0

Related Questions