在JavaScript中使用Trim String方法

news/2024/7/4 19:13:04

It’s always helpful to have an easy method method on strings to remove trailing or leading whitespace and the trim() method available on JavaScript strings is here to help us with doing exactly that.

在字符串上使用简单的方法方法来删除尾部或前导空格总是有帮助的,JavaScript字符串上可用的trim()方法可以帮助我们做到这一点。

Whitespace can anything such as spaces, tabs, no-break spaces, LF (line feeds), CR (carriage returns), etc.

空格可以是诸如空格 , 制表符 , 不间断空格 , LF(换行符) , CR(回车符)等任何内容。

Let’s quickly go over how to use it.

让我们快速了解如何使用它。

String.trim() (String.trim())

The String.trim method is used to remove the whitespace from both ends of a string.

String.trim方法用于删除字符串两端的空格。

  • Parameters: It does not accept any parameter.

    参数:不接受任何参数。
  • Return type: It returns the string without whitespace.

    返回类型:返回不带空格的字符串。

An example of how to use it is shown below 🙈

shown的使用示例如下below

let reptilianString = "     🐊🐊🐊🐊     ";

console.log(reptilianString.trim());

// 🐊🐊🐊🐊

If we want to remove the whitespace only from the front but not from rear or vice-versa, we can also use the following methods.

如果我们只想从前面删除空白,而不是从后面删除空白,反之亦然,我们也可以使用以下方法。

  • trimStart()

    trimStart()

  • trimEnd()

    trimEnd()

trimStart() (trimStart())

The trimStart() method is used to remove whitespace from the start of a string.

trimStart()方法用于从字符串开头删除空格。

  • Parameters: It does not accept any parameter.

    参数:不接受任何参数。
  • Return type: It returns the string without whitespace at the start.

    返回类型:返回开头没有空格的字符串。

Note: trimLeft() is an alias for trimStart()

注: trimLeft()是一个别名trimStart()

trimEnd() (trimEnd())

The trimEnd() method is used to remove the whitespace from the right end of the string.

trimEnd()方法用于从字符串的右端删除空格。

Parameters: It does not accept any parameter. Return type: It returns the string without whitespace on the right end.

参数:不接受任何参数。 返回类型:返回右端无空格的字符串。

Note: trimRight() is an alias for trimEnd()

注: trimRight()是一个别名trimEnd()

翻译自: https://www.digitalocean.com/community/tutorials/js-trim-string-method


http://www.niftyadmin.cn/n/3649750.html

相关文章

[Nutch]如何利用HTML页面中meta元素?

[Nutch]如何利用HTML页面中meta元素?[郑昀]1:假如说你的站点页面中有这么一句:你利用Nutch抓取并分析时,如何得到这个meta数值并处理呢?2:你可以从HtmlParseFilter扩展出一个RobotsParserFilter&#xff0c…

链表中倒数最后k个结点

描述 输入一个长度为 n 的链表,设链表中的元素的值为 ai ,返回该链表中倒数第k个节点。 如果该链表长度小于k,请返回一个长度为 0 的链表。 数据范围:0 \leq n \leq 10^50≤n≤105,0 \leq a_i \leq 10^90≤ai​≤109…

flutter获取地理位置_Flutter:使用Geolocator插件获取用户位置

flutter获取地理位置Ever wanted to get a user’s location within your Flutter application? We’re going to be building an application that does exactly that by taking advantage of the Geolocator plugin. 是否曾经想在Flutter应用程序中获取用户的位置&#xff1…

用户界面和多媒体版面问题[二][j2medev][0406更新]

第一部分在 "用户界面和多媒体"版面问题整理[j2medev.com][不断更新中]。下面是后续更新的部分。全部目录为:1 全屏问题2 Image和ByteArray转换问题3 getRGB问题4 字符串换行问题5 字体问题6 重新播放音乐问题7 MediaException问题8 混音问题9 CustomItem…

手机用户界面和多媒体版面有价值问题整理[j2medev.com][0406更新]

预告:j2medev.com将开始整理各个版面的有价值问题,并以PDF格式集结,敬请期待。出品商产品名称产品版本J2medev.com版面有价值的问题合集0.1j2medev.com之“用户界面和多媒体”版面有价值问题的整理历史VersionDateCreatorDescription1.0.0.12…

golang init_了解Go中的init

golang init介绍 (Introduction) In Go, the predefined init() function sets off a piece of code to run before any other part of your package. This code will execute as soon as the package is imported, and can be used when you need your application to initial…

MIDP设计模式之集结贴[JavaME]

1:架构性宣言: MIDP 2.0 中的工厂设计模式如何使用 MIDP 的 Generic Connection Frameworkhttp://www-128.ibm.com/developerworks/cn/java/wi-arch22/ 级别: 初级Mikko Kontio, 产品经理, Softera2006 年 3 月 13 日这个月将学习有关 MIDP 2.0 的更多知识,同 Mikko…

计网笔记--网络层

1--网络层概述 网络层主要问题: ① 网络层向运输层提供怎样的服务? (可靠或不可靠) ② 网络层寻址问题(IP 地址) ③ 路由选择问题 (路径) 重点在于:TCP/IP协议栈 2--网络…