String objects and Math objects

What is a String object

String object: an object used to process text strings

String object creation

Create a string in a literal way.
字面量的方式创建字符串 var str1="world"; console.log(str1);
Constructor creates a 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"; 
    console.log(str.charAt(0));//B 
    console.log(str.charAt(1));//r 
    console.log(str.charAt(2));//a 
    console.log(str.charAt(3));//v 
    console.log(str.charAt(4));//e 
    console.log(str.charAt(999));//

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

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

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

    var str='hello world!'; 
    console.log(str.search('e'));//1 
    console.log(str.search('a'));//-1 没找到则返回-1

match() matches a character or string that meets the conditions and returns an array

    var str='hello world!'; 
    console.log(str.match('l'));//以数组的形式返回 
    console.log(str.match('a'));//null

replace() replace

    var str='hello world!'; 
    //replace(参数1,参数2); 参数1:替换谁 参数2:替换值 
    console.log(str.replace('l','哈哈'));// he哈哈lo world!

split() string cutting, return array after cutting

    /*split(参数1,参数2) 
    参数1 :必需。字符串或正则表达式,从该参数指定的地方开始切割。 
    参数2 :可选。该参数可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定 的数组。
    如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。*/
    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) Get the string fragment between two index values

    var str='abcdefg'; 
    console.log(str.slice(2,4));//cd 从下标2开始截取,到下标4结束,但不包含4

substr (start position, the length of the interception to obtain the string fragment between the two index values

 var str='abcdefg'; 
    console.log(str.substr(2,3));//cde 从下标2开始截取,截取长度为3的字符串长度

indexOf() Get the first occurrence of a character or string, return the index value if found, return -1 if not found

 var str = 'hello world'; 
    console.log(str.indexOf('o'));//4 
    console.log(str.indexOf('p'));//-1

toLowerCase() converts the string to lowercase

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

toUpperCase() converts the 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

 //找出'o'出现的次数 
 
        var str = 'good morning';
        var num = 0;
        for (var i = 0; i < str.length; i++) {
    
    
            if (str.charAt(i) == 'o') {
    
    
                num++;
            }
        }
        console.log(num);			//3

String output in reverse order

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

What is a Math object

The concept of the
Math object The function of the Math (arithmetic) object is to perform common arithmetic tasks.

Method use of Math object
max(x,y,z…) 返回 x y z …中的最大值。
min(x,y,z…) 返回 x y z …中的最小值。

ceil(x) round up

 console.log(Math.ceil(5.2));//6 
 console.log(Math.ceil(5.5));//6 
 console.log(Math.ceil(5.8));//6

floor(x) rounded down

    console.log(Math.floor(5.2));//5 
    console.log(Math.floor(5.5));//5 
    console.log(Math.floor(5.8));//5

round(x) rounding

 console.log(Math.round(5.2));//5 
 console.log(Math.round(5.5));//6 
 console.log(Math.round(5.8));//6

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

       //随机生成0~1之间的随机数(包括0,但不包括1) 
       console.log(Math.random()); 
       //随机生成0~100之间的随机数(包括0,不包括100) 
       console.log(Math.random()*100); 
       //随机生成5~10之间的随机数 
       console.log(Math.random()*5+5); 
       //随机生成5~10之间的整数 
       console.log(Math.floor(Math.random()*5+5));

Case: Randomly generate an integer within 100, put it into an array, cannot repeat, sort in ascending order

        var arr = [];
        for (var i = 0; i < 100; i++) {
    
    
            var num = Math.floor(Math.random() * 100);    // 随机生成100个数字 
            if (arr.indexOf(num) == -1) {
    
                     // 如果不重复的情况,则存入arr中 
                arr.push(num);
            }
        }
        console.log(arr);
        arr.sort(function (a, b) {
    
                           //小到大排列 
            return a - b;
        });
        console.log(arr);

Guess you like

Origin blog.csdn.net/weixin_46031162/article/details/103973156