Detailed explanation of three methods of JS interception string

Detailed explanation of three methods of JS interception string

JS provides three methods for intercepting strings: slice() , substring() and substr() , which can accept one or two parameters:

var stmp = "rcinn.cn";

Using a parameter

alert(stmp.slice(3)); //Start from the 4th character and intercept to the last character; return "nn.cn"

alert(stmp.substring(3)); //Start from the 4th character and intercept to the last character; return "nn.cn"

Using two parameters

alert(stmp.slice(1,5)) //Starting from the 2nd character, to the 5th character; returns "cinn"

alert(stmp.substring(1,5)); //Starting from the 2nd character, to the 5th character; returns "cinn"

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

alert(stmp.slice(0)); //Return the entire string

alert(stmp.substring(0)); //Return the entire string

Returns the first character

alert(stmp.slice(0,1));//return "r"

alert(stmp.substring(0,1));//return "r"

//In the above example, we can see that the usage of slice() and substring() is the same, and the returned value is the same, but參數為負數時,他們的返回值卻不一樣. See the following example

alert(stmp.slice(2,-5));//return "i"

alert(stmp.substring(2,-5));//return "rc"

//From the above two examples, we can see that slice(2,-5) is actually slice(2,3), negative 5 plus the string length 8 is converted to positive 3 (if the first digit is equal to or greater than the second digit (slice()注意:這里第二位數字如果是負數是加完字符串長度后的數字,而不是顯示的數字例:length=11,(7,-6),-6+11=5第二位小于第一位) , then an empty string is returned); and substring(2,-5) is actually substring(2,0), negative numbers are converted to 0, and substring always takes the smaller number as the starting position.

alert(stmp.substring(1,5)) //Starting from the 2nd character to the 5th character; returns "cinn"

alert(stmp.substr(1,5)); //Starting from the second character, intercept 5 characters; return "cinn."

var phone = 15989012100;

phone.slice(-6) takes the last 6 digits (the second parameter does not need to be written as 0), and returns '012100 ';

phone.slice(-6, -4) takes the last 4 digits to the last 6 digits, (-6+11, -4+11) = (5, 7);

// Compare the date size. When the date is smaller than 1 every month, var nowdate = new Date();
item = 2016-7-16;
temp = item.split('-');
if (temp[0] != curYear || temp[1] != curMonth) {
    return;
}
temp[1] = parseInt(temp[1]) + 1;
date = new Date(temp.join('-'));
if(date>=nowdate){
Execute A;
}else{
Execute B;
}

Replace the letters after the specified string

var abc = 'adadada=ss';
var j = abc.substring(abc.indexOf('=')+1,abc.length);
var dsd = abc.replace(j,'haha'); --> dsd = 'adadada=haha'

The above are three methods of JS string interception introduced by the editor. I hope it will be helpful to everyone. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • JavaScript interception string code example
  • Three common ways to intercept strings in JavaScript: usage differences and example analysis
  • JS string operation example based on regular interception and replacement of specific characters
  • JS regular method to intercept the content between two strings and before and after the strings
  • Detailed explanation of how to use regular expressions to extract strings between two strings in JS
  • Comparison of js string interception functions slice, substring and substr
  • JavaScript interception and cutting string skills
  • Summary of common techniques for JS interception and segmentation of strings
  • Detailed explanation and comparison of Slice, Substring, and Substr functions for intercepting strings in JavaScript
  • Detailed summary of common methods for JS to intercept strings
  • js substring() string interception function
  • Detailed explanation of JavaScript substr() string interception function
  • The difference between JS string interception substr and substring methods
  • Detailed explanation of JS string interception method

<<:  A brief discussion on the color matching skills of web pages (a must-read for front-end developers)

>>:  Let's talk about the size and length limits of various objects in MySQL

Blog    

Recommend

JavaScript to implement mobile signature function

This article shares the specific code of JavaScri...

js to achieve simple accordion effect

This article shares the specific code of js to ac...

Example code of how to create a collapsed header effect using only CSS

Collapsed headers are a great solution for displa...

jQuery achieves seamless scrolling of tables

This article example shares the specific code of ...

Details of watch monitoring properties in Vue

Table of contents 1.watch monitors changes in gen...

Detailed explanation of component communication in react

Table of contents Parent component communicates w...

Example code for implementing complex table headers in html table

Use HTML to create complex tables. Complex tables...

Solution to Nginx SSL certificate configuration error

1. Introduction When a web project is published o...

Example of implementing circular progress bar in Vue

Data display has always been a demand that all wa...

Detailed explanation of the use of Teleport in Vue3

Table of contents Purpose of Teleport How Telepor...

Markup Languages ​​- Lists Again

Click here to return to the 123WORDPRESS.COM HTML ...

Continuous delivery using Jenkins and Docker under Docker

1. What is Continuous Delivery The software produ...

How to compare two database table structures in mysql

During the development and debugging process, it ...