题解 | #参数解析#
参数解析
https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); // 保存所有字符串(所有以空格分开的字符串) let strs = [] // 所有形式正确的参数 const res = [] // 双引号的下标 let [left, right] = [undefined, undefined] rl.on('line', function (line) { strs = line.split(' ') // 如果出现双引号,会出现 [ 'xcopy', '/s', 'c:\\\\', 'd:\\\\e', '"sd', 'sds"' ] // 遍历数组 for(let i = 0; i < strs.length; i++){ // 遇到开始双引号 if(strs[i].startsWith('"') && strs[i].endsWith('"')){ res.push(strs[i].slice(1,-1)) continue }else if(strs[i].startsWith('"')){ left = i continue }else if(left >= 0 && !strs[i].endsWith('"')){ // 双引号直接的内容 continue }else if(strs[i].endsWith('"')){ // 遇到结束双引号 right = i // 开始操作 let tmp = strs[left].slice(1) for(let j = left + 1; j < right; j++){ tmp += ' ' + strs[j] } tmp += ' ' + strs[right].slice(0, -1) res.push(tmp) left = undefined right = undefined continue } res.push(strs[i]) } console.log(res.length) for(let str of res){ console.log(str) } });