Get the App
SLTechnology News&Howtos  ›  Development  › 

How to intercept the last few bits of a string by es6

Shulou Source: shulou.com Published: 2022-05-31 16:29:26 10月04日 Update

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!

Tags: Characters strings methods positions parameters two grammar learning content start differences digits functions that is ideas situations tutorials articles more environment Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Shulou Tech Info Docker Apple OPPO Reno