Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to remove spaces from a string by es6

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge points about how es6 removes the spaces of strings. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

There are two ways to remove spaces in a string: 1. Using trim (), you can remove the space character at the beginning and end of the string, and the syntax "string object .trim ()". 2. Using replace (), you can use regular expressions to remove all spaces in a string, and the syntax "string object .replace (/\ sbig g,")".

The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.

The method of removing spaces in a string by es6

1. Use trim ()-- remove the space character at the beginning and end of the string

The trim () method is used to remove the leading and trailing white space characters of a string, which include: spaces, tabs tab, newline characters, and so on.

Description:

The trim () method does not change the original string.

The trim () method does not apply to null, undefined, Number types.

Example:

Var str = "hello world" console.log ("original string:" + str+ "!"); var newStr=str.trim (); console.log ("new string:" + newStr+ "!")

2. Use replace ()-- remove all spaces in the string

The replace () method is used to replace some characters in a string with other characters, or to replace a substring that matches a regular expression.

If you want to use replace () to remove spaces, you can use regular expressions to match the spaces of a string and then replace them with empty characters''to remove spaces.

Var str = "hello world" console.log ("original string:" + str+ "!"); var newStr=str.replace (/\ newStr+, "); console.log (" new string: "+ newStr+"! ")

These are all the contents of the article "how to remove the spaces of a string in es6". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report