我如何在java中创建一个列表,从foo bar“lorem ipsum”
的输入打印[foo,bar,“lorem ipsum”]
谢谢!
最佳答案
我想通了,有两个列表可以找到值然后将它们合并。
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);
关于java - 创建排除引号内容的空格分隔列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61510190/