JavaScript 删除字符串中的第一个字符
JavaScript 有不同的方法来删除字符串中的第一个字符。由于字符串在 JavaScript 中是不可改变的,所以我们的想法是创建一个新的字符串。下面的每个方法都会有一个代码示例,你可以在你的机器上运行。
substring()
函数是 JavaScript 的一个内置函数。它从给定字符串的起始索引到结束索引返回一个新的字符串。
substring(startIndex, endIndex)
startIndex
是必需的,endIndex
是可选的。如果没有指定 endIndex
,substring()
会选择从 startIndex
到字符串末端的所有字符。
<!DOCTYPE html>
<html>
<head>
<title>
Remove the first character
</title>
</head>
<body>
<h2>
Click on button to display the
`DelftStack` without first character.
</h2>
<button onclick="removeFirstChar()">
Click Button
</button>
<p id="displayString"></p>
<script>
const removeFirstChar = () => {
let str1 = "DelftStack";
let str2 = str1.substr(1);
console.log(str2);
document.getElementById("displayString").innerHTML = str2;
}
</script>
</body>
</html>
通过传递 1
作为参数,函数将返回索引 1
及以后的所有字符。
slice()
方法提取字符串的一部分,并将该部分返回到一个新的字符串中。
slice(startIndex, endIndex)
startIndex
是必需的,endIndex
是可选的。如果没有指定 endIndex
,slice()
会选择从 startIndex
到字符串末端的所有字符。
<!DOCTYPE html>
<html>
<head>
<title>
Remove the first character
</title>
</head>
<body>
<h2>
Click on button to display the
`DelftStack` without first character.
</h2>
<button onclick="removeFirstChar()">
Click Button
</button>
<p id="displayString"></p>
<script>
const removeFirstChar = () => {
let str1 = "DelftStack";
let str2 = str1.slice(1);
console.log(str2);
document.getElementById("displayString").innerHTML = str2;
}
</script>
</body>
</html>
replace()
方法用于用一个新的替换字符串替换字符串的一部分。
replace(paramA, paramB)
paramA
是指定的字符串或正则表达式,来自给定字符串的一部分,我们要用一个新的值来替换,paramB
是一个新的值。两者都是必须的。
这里,我们使用 replace(/^./, "")
来删除第一个字符,因为/^./
表示第一个字符,而""
是空字符串。
<!DOCTYPE html>
<html>
<head>
<title>
Remove the first character
</title>
</head>
<body>
<h2>
Click on button to display the
`DelftStack` without first character.
</h2>
<button onclick="removeFirstChar()">
Click Button
</button>
<p id="displayString">DelftStack</p>
<script>
const removeFirstChar = () => {
let str1 = document.getElementById("displayString").innerHTML;
let str2 = str1.replace(/^./, "");
document.getElementById("displayString").innerHTML = str2;
console.log(str2);
}
</script>
</body>
</html>
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。