String and Math objects

What is a String object

String object: an object used to process text strings

String object creation

Creating a string by literal
Create the string var str1="world"; console.log(str1) in the literal way;
Constructor create string
var str=new String("hellow"); console.log(str);

Method usage of String object

The charAt() method returns the specified character from a string.

 var str = "Brave new world"; 

The concat() method joins one or more strings with the original string to form a new string and returns it.

  var str = "Hello, "; 
  console.log(str.concat(" have a nice day.")); //Hello, have a nice day.  

search() gets the first occurrence of a character or string fragment

    var str='hello world!'; 
    console.log('a'));//-1 if not found, return - 1

match() matches a matching character or string and returns an array

    var str='hello world!'; 
    console.log(str.match('l'));//Return as an array 

replace() replace

    var str='hello world!'; 
    //Replace (parameter 1, parameter 2); parameter 1: replace who parameter 2: replace value 
    console.log(str.replace('l','Ha-ha'));// he ha ha lo world!

split() string cutting, after cutting, return array

    /*split(Parameter 1, parameter 2) 
    Parameter 1: required. String or regular expression, starting at the place specified by this parameter. 
    Parameter 2: optional. This parameter specifies the maximum length of the returned array. If this parameter is set, no more substrings will be returned than the array specified by this parameter.
    If this parameter is not set, the entire string will be split regardless of its length.*/
    var str='good morning'; 
    console.log(str.split());//["good moronioong"] 
    console.log(str.split(''));//["g", "o", "o", "d", " ", "m", "o", "r", "n", "i", "n", "g"] 
    console.log(str.split(' '));//["good", "morning"] 
    console.log(str.split('n'));//["good mor", "i", "g"] 
    console.log(str.split('n',2));//["good mor", "i"]

Slice (start position, end position) gets a string fragment between two index values

    var str='abcdefg'; 
    console.log(str.slice(2,4));//cd starts with subscript 2 and ends with subscript 4, but does not contain 4

Substr (start position, intercepted length gets the string fragment between two index values

 var str='abcdefg'; 
    console.log(str.substr(2,3));//cde starts with subscript 2 and intercepts the string length of 3

indexOf() gets the first occurrence of a character or string. If it is found, it returns the index value. If it is not found, it returns - 1

 var str = 'hello world'; 

toLowerCase() converts a string to lowercase

var str='Hellow World'; 
    console.log(str.toLowerCase());//hellow world

toUpperCase() converts a string to uppercase

  var str='Hellow World'; 
    console.log(str.toUpperCase());//HELLOW WORLD

Find out the number of occurrences of a character in a string

 //Find out how many times' o 'occurs 
        var str = 'good morning';
        var num = 0;
        for (var i = 0; i < str.length; i++) {
            if (str.charAt(i) == 'o') {
        console.log(num);			//3

String reverse output

   var str='good morning'; 
     console.log(str.split('').reverse().join(''))//gninrom doog

What is a Math object

The concept of Math object
The Math object is used to perform common arithmetic tasks.

Method usage of Math object
max(x,y,z... )Back to x y z The maximum value in.
min(x,y,z... )Back to x y z The minimum value in.

ceil(x) rounding up


floor(x) rounding down


round(x) rounding


random() returns a random number between 0 and 1, containing 0 but not 1

       //Randomly generate a random number between 0 and 1 (including 0, but excluding 1) 
       //Randomly generate a random number between 0 and 100 (including 0, excluding 100) 
       //Random number between 5 and 10 
       //Randomly generate integers between 5 and 10 

Case, randomly generate an integer within 100, put it into an array, can't repeat, sort in ascending order
        var arr = [];
        for (var i = 0; i < 100; i++) {
            var num = Math.floor(Math.random() * 100);    // Randomly generate 100 numbers 
            if (arr.indexOf(num) == -1) {                 // If it is not repeated, it will be stored in arr 
        arr.sort(function (a, b) {                       //Small to large permutations 
            return a - b;

Published 6 original articles, won praise 0, visited 15
Private letter follow

Tags: Fragment

Posted on Mon, 13 Jan 2020 23:52:05 -0800 by JOWP