题解 | #参数解析#

参数解析

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

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 14:35
点赞 评论 收藏
分享
找个工作&nbsp;学历是要卡的&nbsp;要求是高的&nbsp;技能不足是真的&nbsp;实习经验是0的&nbsp;简历无处可写是事实的&nbsp;钱不好赚是真的&nbsp;想躺平又不敢躺&nbsp;也不甘心躺&nbsp;怕自己的灵感和才华被掩埋甚至从未被自己发现&nbsp;又质疑自己是否真正有才华
码农索隆:你现在啊,你心里都明白咋回事,但是你没办法改变现状,一想到未来,你又没有信心狠下心来在当下努力。 得走出这种状态,不能一直困在那里面,哪不行就去提升哪,你一动不动那指定改变不了未来,动起来,积少成多才能越来越好
点赞 评论 收藏
分享
半解316:内容充实,细节需要修改一下。 1,整体压缩为一页。所有内容顶格。 2,项目描述删除,直接写个人工作量 修改完之后还需要建议,可以私聊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务