java创建以空格分隔的列表,其中不包括引号的内容 1 周,5 日 Questions & Answers 30 我如何用java创建一个列表,从foo bar "lorem ipsum"的输入中打印[foo,bar,"lorem ipsum"]谢谢
# 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);
# 1 楼答案
我想出来了,有两个列表可以找到值,然后合并它们