【问题标题】:how to replace the position of the letter in a word using js? [duplicate]如何使用js替换单词中字母的位置? [复制]
【发布时间】:2021-01-28 17:46:06
【问题描述】:

让我们说一句话 = 我要输出=

ibrd

我应该把它的位置改到开头。 我可以使用 JavaScript 做到这一点吗? 如何使用字符串来做到这一点。

【问题讨论】:

标签: javascript string


【解决方案1】:

我想这会解决你的问题

const changeLetter = (val /* string value */, index /* index of the letter (i in this scenario)*/) => {
  let temp = val.split('');

  temp.splice(index, 1);
  temp.unshift(val[index]);

  return temp.join('');
}

const val = 'bird';

console.log(changeLetter(val, 1));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-06
    • 2017-05-16
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 2014-08-11
    • 2020-06-16
    • 1970-01-01
    相关资源
    最近更新 更多