【问题标题】:Adding a whitespace inside a List of strings?在字符串列表中添加空格?
【发布时间】:2021-06-08 10:20:37
【问题描述】:

我有一个包含不同单词的字符串ArrayList。我要做的是在每个单词之间的列表中添加空格。

我的代码确实打印出带有空格的元素,但仅作为打印输出,我需要将空格包含在列表中,以便我可以将其用作值。

示例输入:Hi, how are you?,结果:Hi,", " ", "are", " ", "you?

public void getCipher(ArrayList<String> list) {
    String regex = "\", \" " + "\", " + "\"";
    for (String input : list) {
        String newResult = input + regex;
        System.out.print(newResult);
    }
}

【问题讨论】:

    标签: java string arraylist character whitespace


    【解决方案1】:

    迭代列表并使用新字符串更新其中的每个字符串,如下所示:

    public List<String> updateStrings(List<String> list) {
        String regex = "\", \" " + "\", " + "\"";
        for (int i = 0; i < list.size(); i++) {
            list.set(i, list.get(i) + regex);
    
        }
        return list;
    }
    

    Test it online:

    import java.util.ArrayList;
    import java.util.List;
    
    public class Main {
        public static void main(String[] args) {
            // Test
            List<String> list = new ArrayList<>(List.of("Hello", "World"));
            System.out.println(list);
            System.out.println(updateStrings(list));
        }
    
        static List<String> updateStrings(List<String> list) {
            String regex = "\", \" " + "\", " + "\"";
            for (int i = 0; i < list.size(); i++) {
                list.set(i, list.get(i) + regex);
    
            }
            return list;
        }
    }
    

    输出:

    [Hello, World]
    [Hello", " ", ", World", " ", "]
    

    【讨论】:

    • 这很好,但我也有正则表达式的问题,我希望我的输出用引号覆盖每个单词,从头到尾都有空格。比如“你好”、“”、“世界”。
    • @Waelalmerri - 在这种情况下,您可以使用 String regex = "\", \" \""; for (int i = 0; i &lt; list.size(); i++) { list.set(i, "\"" + list.get(i) + regex); }。如有任何疑问,请随时发表评论。
    【解决方案2】:

    要在列表中的其他项目之间添加空格,我会创建一个新列表并遍历“旧”列表,如下所示:

    public ArrayList<String> getCipher(ArrayList<String> list) {
        ArrayList<String> result = new ArrayList<>();
        String regex = "\", \" " + "\", "+ "\"";
        for (String input : list) {
            result.add(input + regex);
            result.add(" ");
        }
        return result;
    }
    

    您可以将列表设置为此方法的结果,例如

    list = getCipher(list)
    

    【讨论】:

      【解决方案3】:

      您可以使用flatMap 方法在列表中的元素之间插入空格:

      public static void main(String[] args) {
          List<String> list = List.of("Hi,", "how", "are", "you?");
          System.out.println(appendSpaces(list));
      }
      
      public static List<String> appendSpaces(List<String> list) {
          return list.stream()
                  // append a space before each word
                  .flatMap(str -> Stream.of(" ", str))
                  // skip first space
                  .skip(1)
                  // return list
                  .collect(Collectors.toList());
      }
      

      输出:

      [Hi,,  , how,  , are,  , you?]
      

      创意截图:


      另见:How to add characters in the middle of a word in an arraylist in java?

      【讨论】:

        猜你喜欢
        • 2012-04-16
        • 2019-10-11
        • 1970-01-01
        • 2020-09-16
        • 1970-01-01
        • 1970-01-01
        • 2013-02-04
        • 1970-01-01
        • 2016-09-18
        相关资源
        最近更新 更多