Two ways to intercept the first digits of a string in js: slice()&substring()

When using strings in Javascript, we do not necessarily need all the strings. In this case, we need to intercept the strings. This article mainly introduces two methods for intercepting the first digits of a string in js: 1. Using the slice() method; 2. Using the substring() method.

Method 1: Use slice() method

Extracts a portion of a string and returns the extracted portion as a new string.

grammar

string.slice(start,end)

Example

var str="abc12345";
  
alert(str.slice(3))   //   =>  12345
  
截取掉前三位字符,保留后几位

Method 2: Use the substring() method

Used to extract characters between two specified subscripts in a string.

grammar

string.substring(start,stop)

Example

var disName ='开心一族漂亮家园';

var shortName = disName.substring(0,5);

console.log(shortName);//打印结果:开心一族漂

Guess you like

Origin blog.csdn.net/Maxueyingying/article/details/127527172