有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    我想出来了,有两个列表可以找到值,然后合并它们

                String message = "foo bar "lorem ipsum";
                // Getting the value of the quotes
                List<String> quotes = new ArrayList<String>();
                Matcher m = Pattern.compile("\\\"([^\\\"]*)\\\"")
                        .matcher(message);
                while (m.find()) {
                    quotes.add(m.group());
                }
                // Removing quotes from string
                message = message.replaceAll("\\\"([^\\\"]*)\\\"", "");
                List<String> space = Arrays.asList(message.split("\\s* \\s*"));
                // Merging the lists
                List<String> newList = Stream.concat(space.stream(), quotes.stream())
                        .collect(Collectors.toList());
                // Printing
                System.out.println(newList);