javascript - 将字符串中的第一个字母放在字符串的末尾

标签 javascript html string

我试图将字符串的第一个字母定位到该字符串的末尾。这是我的代码。

document.getElementById("modify").addEventListener("click", switchLetter);
    
function switchLetter () {
  var input = document.getElementById("input").value;
  input = input.split("");
}
Type a String, And I will Modify it:<br />
<input type="text" id="input" />
<input type="text" id="output" /><br />

<button id="modify">Modify</button>

此时input[0]会返回输入字符串的第一个字母。我试着做 input[0].reverse();但这似乎不正确。

有什么想法吗?

最佳答案

您可以使用 substr .它需要 2 个参数,第一个是您的起始位置,第二个是长度。

function switchLetter () {
  var input = document.getElementById("input").value;
  document.getElementById("input").value = input.substr(1) + input.substr(0,1);
}
<input type="text" id="input"><button onclick="switchLetter()">Switch!</button>

关于javascript - 将字符串中的第一个字母放在字符串的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43190684/

相关文章:

php - 需要 php 来编码特殊字符而不是 html 标签,以便包含在 wordpress 扩展的 rss 文件中

python - 为什么此 string.punctuation 代码不适用于剥离标点符号?

javascript - 不使用 JavaScript 下载文件

javascript - .load 和 .unload div 内的外部页面

javascript - 倒计时不断变化

javascript - Rx.Observable.prototype.skip 未定义

javascript - 如何在网站中运行 SWF 文件而不先完成加载

jquery - 显示隐藏的 div 时停止页面滚动

c - char数组c中的数字和字母

regex - 使用逗号而不是空格将 Delphi 字符串自动换行到一定长度?