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 a part of a string in js

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

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to intercept a certain part of a string in js". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to intercept a certain part of a string in js.

JS provides three methods to intercept strings: slice (), substring (), and substr (), all of which can accept one or two parameters.

JS intercepts strings

Varstmp= "rcinn.cn"

Use a parameter

Alert (stmp.slice (3)); / / start with the fourth character and truncate to the last character; return "nn.cn"

Alert (stmp.substring (3)); / / start with the fourth character and truncate to the last character; return "nn.cn"

Use two parameters

Alert (stmp.slice (1p5)) / / starts from the second character to the fifth character; returns "cinn"

Alert (stmp.substring (1p5)); / / from the second character to the fifth character; returns "cinn"

If only one parameter is used and it is 0, the entire parameter is returned.

Alert (stmp.slice (0)); / / returns the entire string

Alert (stmp.substring (0)); / / returns the entire string

Returns the first character

Alert (stmp.slice (0Pol 1)); / / returns "r"

Alert (stmp.substring (0Pol 1)); / / returns "r"

JS intercept string example

/ / in the above example, we can see that the use of slice () and substring () are the same, and the value returned is the same, but when the parameter is negative, their return value is different. See the following example.

Alert (stmp.slice (2maim 5)); / / return "I"

Alert (stmp.substring (2maimei 5)); / / return "rc"

/ / from the above two examples, we can see that slice (2maime5) is actually slice (2pime3). Minus 5 plus string length 8 is converted into positive 3 (if the first digit is equal to or greater than the second digit (slice ()) Note: here the second digit is the number after adding the string length. Instead of displaying a number example: length=11, (7),-6, 11, 5, the second bit is less than the first bit), an empty string is returned) While substring (2centure 5) is actually substring (2Magin0), the conversion of negative numbers to 0quotient substring always takes a smaller number as the starting position.

Alert (stmp.substring (1p5)) / / starts from the second character to the fifth character; returns "cinn"

Alert (stmp.substr (1p5)); / / intercept 5 characters from the second character; return "cinn."

Varphone=15989012100

Phone.slice (- 6) takes the last 6 digits (no need to write 0 for the second parameter), return'012100'

Phone.slice (- 6memmel 4) takes the last 4 to the last 6 digits, (- 61414141411) = (51997)

At this point, I believe you have a deeper understanding of "how to intercept a certain part of a string in js". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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