首页 > 技术文章 > String 内置对象和Math对象

52580587zl 2020-04-11 20:21 原文

0411

String 内置对象

   //创建字符串的时候要使用引号,单引号双引号都可以,多层引号必须要切换使用。如果有多层,可以使用转义

   例:

var str = 'hello "xi\'ao\'ming"'

   var str1 = new String(‘hello string’)

   var str2 = str + str1     //字符串拼接

 

  //字符串比较大小是按位数比较,第一位比完再比第二位。

单个字符都同有自己的ASCII编码值,但是ASCII能表示的范围0-255总共只有256个编码.

     unicode是另一种编码方式,它可以表示更多的字符,

  中文\u4e00 - \u9fa5

  中文在unicode里的编码顺序和汉语字典的顺序一致,汉字越靠后就越大。

 

字符串(String)中常见的API。API:系统提供的方

   字符串也有索引和length。空格也是有意义的字符。

在算字符串的length时,要加上空格的长度。

     遍历字符串:

    

forvar i = 0;i<str.length;i++){console.log(str[i])}

     例:var str = ‘hello string’

 

1.charAt()返回字符串指定位置的字符

   例:

console.log(str.charAt(1))  // e
       console.log(str[1])    //也为e

 

2.indexOf()从左往右找,返回的是索引值(下标)

   例:

console.log (str.indexOf(‘l’)) // 2

3.lastIndexOf()从右往左找,返回的是索引

 例:

console.log(str.lastIndexOf('l')) // 3

    console.log(str.indexOf('z')) // -1
    console.log(str.lastIndexOf('z')) // -1

 

4.charCodeAt()返回字符的编码

例:

  console.log(str. charCodeAt(1))        

 //101.字母e的编码101

 

5.formCharCode()传一个编码,得到字符

例:

console.log(string. formCharCode(102)) // f

 

6.concat() 字符串拼接(不常用)

例:

var str1 = str.concat(‘123’)

 

7.replace() 替换.如果要替换多个关键字,要结合正则

例: 

var str = ‘hello TMD string’
      var str1 = str.replace(‘TMD’,’***’)

8.slice( ) 从字符串中截取片段,只有一个参数时,从头到尾。负数是倒数第几个。

例:

var str1 = str.slice(2,5)

//包含第一个参数,不包含第二个参数。

   9.split() 和数组的join是相反的,将字符串切割成数组。

     例:

var arr = str.split()//

 

   10.toUpperCase()把字符串全部转为大写

   11.toLowerCase()  把字符串全部转为小写

12.trim() 只将字符串的前后空格去掉

ES6新增的字符串API

startsWith()

字符串是否以某个字符开头(ES6)

endsWith()

字符串是否以某个字符结尾(ES6)

includes()

字符串是否包含某个字符(ES6)

repeat()

重复某个字符串几次(ES6)

Math对象:

  Math对象上的属性都是常量,不是变量,不能被修改。

Math对象方法:

   常用的Math的API

   1、abs()取绝对值

     例:

console.log(Math.abs(-4)) // 4

   2.ceil()向上取整,

     例:

        console.log(Math.ceil(3.7)) // 4
        console.log(Math.ceil(-3.0001)) //-3

 

   3.floor()向下取整

     例:

console.log(Math.floor(3.7)) // 3

    console.log(Math. floor (-3.7)) // -4

    console.log(Math. floor (3.4444)) // 3

 

   4.round()四舍五入取整

      例:

            console.log(Math.round(3.6))  // 4
         console.log(Math.round(3.3))  // 3
           console.log(Math.round(-3.5))  // -3

 

   不常用的Math的API

   1.sin() 取正弦,

   2.cos() 取余弦

   3.tan() 取正切

  注意:三角函数参数的数值传弧度而不是角度。完整的圆的角度为360度。360角度===2PI弧度

   4.max()拿到最大值,可以传多个值

      例:

console.log(Math.max(4,8)) // 8

   5.min()拿到最小值,可以传多个值

     例:

console.log(Math.min(4,8)) // 4

6.pow(x,y)返回x的y次幂

      例:

console.log(Math.pow(2,3))   //2的3次方为8

   7.sqrt(x)返回数的算术平方根

      例:

console.log(Math.sqrt(4)) // 4的算术平方根是2

最常用的Math的API

    random()返回0-1之间的随机小数,理论上包含0不包含1

      例:

        console.log(Math.random())

        var num = Math.ceil(Math.random()*10)//1-10

        var num = Math.floor(Math.random()*10)//0-9

 

虽然这样可以实现0-10,但0和10出现的几率比其他数字小一半。不常用。所以一半随机数不会用四舍五入。

        

var num = Math.round(Math.random()*10)//0-10

     //例:生成一个50-100的随机数,包含50不包含100

           Math.floor(Math.random()*50) + 50

     //生成任意之间的随机数,min-max

        Math.random()*(max-min) + min

 

推荐阅读