javascript - 如何交换字符串 JavaScript 中的字符位置
问题描述
我正在制作一个解密功能,但我被困在需要交换字符串的第二个字母和最后一个字母的位置的部分。 我也尝试过使用替换方法,但我认为应该使用子字符串。
Hello 应该等于 Holle 等
function decipher(str) {
let s = ""
for (let word of str.split(" ")){
let dig = word.match(/\d+/)[0]
word = word.replace(dig, String.fromCharCode(dig))
let secondLetter = word[1]
let lastLetter = word[word.length - 1]
let swapped = word.substring(0,1) + lastLetter + word.substring(2,3) + secondLetter
s += swapped + " "
}
return s
};
解决方案
请更改此行:
let swapped = word.substring(0,1) + lastLetter + word.substring(2,word.length - 1) + secondLetter;
推荐阅读
- r - R根据列名拆分将数据框列转换为单列
- android - 材料设计自定义图标颜色
- macos - 如何在 mac 上设置 java_opts 环境变量?
- snowflake-cloud-data-platform - GRANT IMPORTED PRIVILEGES ON DATABASE - 限制对象
- module - 在 Pascal 中的另一个文件夹上使用单元
- python - to_numpy 没有在 python 中返回浮点数
- sql - 使用 SQL 进行平均插补
- r - 如何根据其他列的条件更新名称(清理数据)
- reactjs - 在生产中更改 React 应用程序的标题和描述
- python - 打开 Jupyter Notebook (2020) 时 Python 停止运行