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 intercept the last few bits of a string by es6

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "es6 how to intercept the last few bits of a string". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "es6 how to intercept the last few bits of a string".

There are four methods: (1) using "str.charAt (str.length-1)" to intercept the last bit of a string; (2) using "str.substr (str.length-N)" to intercept the last N bit; (3) using "str.slice (str.length-N)" to intercept the last N bit and so on.

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

The method of intercepting the last few bits of a string by es6

Method 1: use charAt () to intercept the last 1 bit

The charAt method returns a character at the specified position

Var str= "123456"; console.log (str); var c=str.charAt (str.length-1); console.log ("last bit:" + c)

Method 2: use substr () to intercept the post N bit

The substr method can extract characters of a specified length from the start position from the character, syntax:

Str.substr (str.length-N)

If you want to intercept the last few bits of a string, the parameter N is set to several.

Var str= "123456"; console.log (str); console.log ("Last 1:" + str.substr (str.length-1)); console.log ("Last 2:" + str.substr (str.length-2)); console.log ("Last 3:" + str.substr (str.length-3)); console.log ("Last 4:" + str.substr (str.length-4))

Method 3: use slice () to intercept the post N bit

The two parameters of slice represent the start position and the end position, including the start position and not the end position, respectively. If omitted, it is indicated to the end.

Syntax:

Str.slice (str.length-N)

If you want to intercept the last few bits of a string, the parameter N is set to several.

Var str= "123456"; console.log (str); console.log ("Last 1:" + str.slice (str.length-1)); console.log ("Last 2:" + str.slice (str.length-2)); console.log ("Last 3:" + str.slice (str.length-3)); console.log ("Last 4:" + str.slice (str.length-4)); console.log ("Last 5:" + str.slice (str.length-5))

Method 4: use substring to intercept the N bits

The str.substring (start, end) method also intercepts the string, and the two parameters represent the number of digits at the beginning and the end, similar to slice, but the difference is that substring does not accept negative numbers, and if the start value is greater than the end value, the two positions are automatically exchanged.

Syntax:

Str.substring (str.length-N))

If you want to intercept the last few bits of a string, the parameter N is set to several.

Var str= "123456"; console.log (str); console.log ("last 1 bit:" + str.substring (str.length-1)); console.log ("last 2 bit:" + str.substring (str.length-2)); console.log ("last 3 bit:" + str.substring (str.length-3)

Thank you for reading, the above is the content of "how es6 intercepts the last few bits of a string". After the study of this article, I believe you have a deeper understanding of how es6 intercepts the last few bits of a string, and the specific use still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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