JavaScript 中的左修剪字符串
当我们向客户询问信息时,我们经常会处理字符串。 正因为如此,程序员被迫处理许多输入字符串,这些字符串有时不一致并且可能包含空格或其他不寻常的字符。
在本文中,我们将介绍如何消除字符串左侧的空格。 它可以通过多种方式实现。
本文将使用 replace()
函数以及正则表达式工具。
首先,我们将创建一个不一致的测试字符串,方法是声明一个名为 name 的变量,并为其分配一个由位于名称开头之前的五个空格组成的值,就像下面的代码一样。
let name = " John Doe";
每个示例程序都将使用名称变量。
使用正则表达式和 replace() 函数在 JavaScript 中左修剪字符串
现在我们可以访问 name 变量,我们可以开始使用它了。 要删除位于第一个字符之前的所有空格,我们将对字符串使用内置的 replace()
函数和我们的正则表达式,然后将结果文本存储在结果变量中。
let result = name.replace(/^\s+/, "");
replace() 方法需要来自用户的两段输入。 第一个参数是需要替换的字符串或正则表达式,第二个参数是为第一个参数请求的替换,在我们的例子中是一个空字符串。
第一个参数是需要替换的字符串或正则表达式。
可以将与 replace()
函数一起使用的正则表达式描述为:
-
^
代表字符串的开头 -
\s
匹配空格 -
+
表示一个或多个
我们需要做的最后一件事是检查是否一切正常,因为它应该将输出打印到我们的控制台。 下面是它的完整代码。
let name = " John Doe";
let result = name.replace(/^\s+/, "");
console.log(result);
输出:
John Doe
在 JavaScript 中使用 trimLeft() 或 trimStart() 函数来左修剪字符串
2019 年,当 ECMAScript 10 可用时,我们获得了一个名为 trimStart()
的函数。 此方法是字符串类的一部分,它会从字符串中删除前导空格,同时仅修剪字符串的前导部分。
可以直接对名称变量进行调用。
let result = name.trimStart();
当我们想要相同的结果时,我们可以使用类似的技术 trimLeft()
。
let result = name.trimLeft();
下面给出了 trimLeft()
的完整代码。
let name = " John Doe";
let result = name.trimLeft();
console.log(result);
输出:
John Doe
在 JavaScript 中使用带有 substring() 函数的 while 循环来左修剪字符串
要从文本的开头删除空格,我们可以使用带有 substring()
函数的 while 循环。
我们将使用一个名为 index 的变量来跟踪空格的数量,并在定义它时将其赋值为 0。
let index = 0;
在某种程度上,字符串也被认为是 JavaScript 中的对象。 它表明该字符串是字符索引的集合。
字符串可以像数组一样循环。 我们将使用 while 循环遍历我们的名称字符串,对于每个空格,我们将向索引变量添加 1。
while (name[index] == " ") {
index++;
}
现在我们有了名称字符串和索引作为名称开始前的空格数。
我们现在可以调用 substring()
函数并将索引作为空格的计数传递给它,以取回没有空格的字符串并将其保存在结果变量中。
let result = name.substring(index);
最后,我们将在控制台上显示结果。 下面给出了此方法的代码。
let name = " John Doe";
let index = 0;
while (name[index] == " ") {
index++;
}
let result = name.substring(index);
console.log(result);
输出:
John Doe
此处说明的函数与所有主要浏览器和 JavaScript 运行时兼容。
相关文章
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 事件。