HJ46 截取字符串

65 阅读1分钟

image.png

const rl = require('readline').createInterface({ input: process.stdin })
var iter = rl[Symbol.asyncIterator]()
const readline = async () => (await iter.next()).value

// 用于存放输入的值
let lines = []
void (async function () {
  while ((line = await readline())) {
    // Write your code here
    // 将输入的值放进数组中
    lines.push(line)
    // 如果输入的是两组数据
    if (lines.length === 2) {
      // 获取输入的字符串
      let str = lines[0]
      // 获取要截取的长度
      let k = parseInt(lines[1])
      console.log(str.slice(0, k))
    }
  }
})()

截取字符串_牛客题霸_牛客网 (nowcoder.com)