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

Recommend

MySQL 8.0.20 installation and configuration detailed tutorial

This article shares with you a detailed tutorial ...

How to allow all hosts to access mysql

1. Change the Host field value of a record in the...

Methods and steps to upgrade MySql5.x to MySql8.x

Several Differences Between MySQL 5.x and MySQL 8...

Vue+spring boot realizes the verification code function

This article example shares the specific code of ...

Problems and solutions encountered when connecting node to mysql database

I installed a new version of MySQL (8.0.21) today...

Detailed explanation of the command mode in Javascript practice

Table of contents definition structure Examples C...

Detailed introduction to Mysql date query

Query the current date SELECT CURRENT_DATE(); SEL...

Methods to enhance access control security in Linux kernel

background Some time ago, our project team was he...

js to achieve simple image drag effect

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

Solution to the problem of passing values ​​between html pages

The first time I used the essay, I felt quite awkw...

How to stop CSS animation midway and maintain the posture

Preface I once encountered a difficult problem. I...

How to implement insert if none and update if yes in MySql

summary In some scenarios, there may be such a re...