简体   繁体   English

如何在Java中将字符串数组拆分为单独的数组

[英]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

Do you mean like this, using Arrays.deepToString() to print nested arrays? 使用Arrays.deepToString()来打印嵌套数组,您是不是这样想?

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(" ", "], [") + "]");

Here is one way you could build the String[][] : 这是构建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. 正如您所注意到的,它不会简单地将数组传递给println,因为它的默认toString方法不会显示其元素。 Therefore, use the Arrays.toString or Arrays.deepToString methods to print an arrays elements. 因此,使用Arrays.toString或Arrays.deepToString方法来打印数组元素。

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);
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]

If you want to simply print it then, it is very simple to do, using the streams api. 如果您只想打印它,那么使用streams api非常简单。 Here is an example: 这是一个例子:

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

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM