es6 Learn to summarize string and number operations

Posted by biopv on Thu, 21 Nov 2019 09:05:33 +0100

String Lookup

ES6 also adds string lookup and supports Chinese

Find under es5: indexof()

let jspang='technology';
let blog = 'I'm glad you read this article. I'm your old friend's technology.In this lesson, we'll learn string templates.';
document.write(blog.indexOf(jspang));

Result: 20

Find under es6: include ()

let jspang='technology';
let blog = 'I'm glad you read this article. I'm your old friend's technology.In this lesson, we'll learn string templates.';
document.write(blog.includes(jspang));

Result:true

Determine if startsWith() exists at the beginning

Return value is Boolean type

let jspang='Technical fat';
let blog = 'I'm glad you read this article. I'm your old friend, Technical Fat.In this lesson, we'll learn string templates.';

blog.startsWith(jspang); //false

Determine if endsWith() exists at the end

    let jspang='Technical fat';
    let blog = 'I'm glad you read this article. I'm your old friend, Technical Fat.In this lesson, we'll learn string templates.';

    blog.endsWidth(jspang); //false

Note that both starts and ends should be followed by s

Substring Recognition of Strings

  • includes() method that returns true if the specified text is detected in the string, or false otherwise.
  • The startsWith() method returns true if the specified text is detected in the actual part of the string, or false otherwise.
  • endsWhit() method that returns true if the specified text is detected at the end of the string, or false otherwise.

Both of the above three methods can accept two parameters:

  • The first parameter specifies the text to search for;
  • The second parameter is optional, specifying a starting search location.
  • If the second parameter is specified, the includes method and the startsWith method will start matching from this miniature value, and the endsWith method will start matching forward by subtracting the length of the text to be searched from this index value.
  • Examples are as follows:

let msg = "hello world!";

//judge
console.log(msg.includes('hello'));
//Judgement End
console.log(msg.endsWith('!'));
//Judgment Start
console.log(msg.startsWith('h'));
console.log(msg.startsWith('0'));
console.log(msg.endsWith('world!'));
console.log(msg.includes('x'));

console.log(msg.startsWith('o',4));
console.log(msg.endsWith('o',8));
console.log(msg.includes('0',8));

Output Results

➜  ES6 Practice git:(master) ✗ node string.js
true
true
true
false
true
false

true
true
false

Summary: When all three methods have two parameters, they all play a role in the search function. StartsWithand endsWithnot only exist at the beginning or the end, but also true. StartsWithis the beginning or not of the second parameter, while endsWithis the second parameter minus the length of the text you want to search and starts to match forward

repeat() method

es6 provides a repeat method for a string that accepts a numbered parameter indicating the number of times the string has been repeated, and the return value is a certain number of times the current string has been repeated, as shown in the following example:

console.log('x'.repeat(2));
console.log('hello'.repeat(3));

Output Results

xx
hellohellohello

ES6 Digital Operation

Number Judgment and Conversion

Number.isFinite (xx) for numeric validation

Determine if it is a number
Number.isFinite() can be used for numeric validation, returning true for any number, whether floating-point or reshaping, and false for others.

let a= 11/4;
let b=11
let c=3.1475926
console.log(Number.isFinite(b)); //true
console.log(Number.isFinite(c));//true
console.log(Number.isFinite(a));//true
console.log(Number.isFinite('jspang'));//false
console.log(Number.isFinite(NaN));//false
console.log(Number.isFinite(undefined));//false

NaN Validation

NaN is a special non-number that can be verified using Number.isNaN().The code console below returns true.

console.log(Number.isNaN(NaN));
console.log(NaN == NaN); //false
console.log(NaN === NaN);//false
console.log(isNaN(NaN));//true

Determine if it is an integer Number.isInteger(xx)

let a=123.1;
console.log(Number.isInteger(a)); //false

Integer conversion Number.parseInt(xxx) and floating-point conversion Number.parseFloat (xxxx)

let a='9.18';
console.log(Number.parseInt(a));  //9
console.log(Number.parseFloat(a)); //9.18

Integer range operation

Topics: Javascript git