题解 | #参数解析#
参数解析
https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); boolean flag = true; //true为正常模式 StringBuilder sb = new StringBuilder(); List<String> list = new ArrayList<>(); for (int i = 0; i < s.length(); i++) { Character ch = s.charAt(i); // 遇到“则开启和关闭保留空格模式 if (ch == '\"') { flag = !flag; continue; } if (flag) { //正常情况下遇到空格就新增 if (" ".equals(String.valueOf(ch))) { list.add(sb.toString()); sb = new StringBuilder(); } else { sb.append(ch); } } else { // 如果开启了空格保留模式,遇到空格也不新增 sb.append(ch); } } list.add(sb.toString());//最后一个字符串 System.out.println(list.size()); for (String str : list) { System.out.println(str); } } }