【问题标题】:How to split a string array into separate arrays in Java如何在Java中将字符串数组拆分为单独的数组
【发布时间】:2016-12-26 17:40:55
【问题描述】:

我正在尝试将我的数组拆分为单独的数组。

我有一串单词。我将单词分成一个数组。现在我正在尝试将单词数组拆分为它们自己的单独数组。

例子:

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

我希望我的输出是:

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

这是我目前所拥有的:

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

这是我的输出:

[Ljava.lang.String;@63947c6b

【问题讨论】:

  • 你的意思是每个wordarraychars|strings?
  • 如果它是一个数组数组,那么您的输出将输出为:[[This], [is], [a], [string]]。所以你更关心数据结构还是输出?
  • 让您感到困惑的一半是数组上的toString() 看起来像[Ljava.lang.String;@63947c6b。如果你这样做了System.out.println(Arrays.toString(wordsArray.split(" "));,你就不会那么困惑了。
  • 你的问题不清楚。为什么你想要一个包含单个元素的数组?

标签: java arrays string split


【解决方案1】:

你的意思是这样,使用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));

输出

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

如果您只想构建您列出的字符串,那么这是一种更简单的方法:

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

输出

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

或使用流在单个语句中相同:

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

或者你可以作弊然后这样做:

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

【讨论】:

  • 最后一个... LOL :D
【解决方案2】:

这是构建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);
}

正如您所注意到的,我们不会简单地将数组传递给 println,因为它的默认 toString 方法不会显示它的元素。因此,请使用 Arrays.toString 或 Arrays.deepToString 方法打印数组元素。

【讨论】:

    【解决方案3】:

    你可以这样做

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

    【讨论】:

      【解决方案4】:
      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 + "]");
      

      生产:

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

      【讨论】:

        【解决方案5】:

        它可以简单地做到:

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

        【讨论】:

          【解决方案6】:

          如果您想简单地打印它,那么使用流 api 非常简单。这是一个例子:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-04-01
            • 2019-03-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多