题解 | #参数解析#

参数解析

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)
    }
});

全部评论

相关推荐

01-23 19:12
门头沟学院 Java
榨出爱国基因:你还差 0.1% 就拿到校招礼盒,快叫朋友给你砍一刀吧
投递拼多多集团-PDD等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务