题解 | #参数解析#
参数解析
https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(scan.hasNextLine()) {
String line = scan.nextLine();
//连续空格合并
line = line.replaceAll("\\s+", " ").trim();
//System.out.println(line);
char[] arr =line.toCharArray();
boolean inYinhao = false;
ArrayList<String> params = new ArrayList();
StringBuilder sbl = new StringBuilder();
for(char c:arr) {
if(c == '"') {
inYinhao = !inYinhao;
continue;
}
if(c==' '&&!inYinhao) { // 碰到空格,一般要提交命令参数
params.add(sbl.toString());
sbl = new StringBuilder();
continue;
}
sbl.append(c);
}
//结尾没有空格,所以最后一个参数这里要这样加下
params.add(sbl.toString());
System.out.println(params.size());
for(String s:params) {
System.out.println(s);
}
}
}
}