javascript 内置对象(字符串)

1.字符串的创建方式

1、使用直接字面量

let str = 'hello js'

2、使用String构造函数

let str1 = new String('tom')

3、ES6模板字符串

let str3 = `<div class="content">${str}</div>`

      function html(str, selector) {
        // 比较古老的获得元素的方法
        document.querySelector(selector).innerHTML = str
      }
      html(str3, '.box>h3')
      console.log(str, str1)

2.字符串的常用方法

1、字符串的长度

function fn1() {
        let str = '字符串常用的方法'
        console.log(str.length) // 8
      }
      // fn1()

2、charAt(index) 提取指定index位置的字符

 function fn2() {
        let str = '字符串常用的方法'
        console.log(str.charAt(0)) // 字
        let arr = []
        for (let i = 0; i < str.length; i++) {
          // arr.push(str.charAt(i))
          arr.push(str[i])
        }
        console.log(arr)
      }
      // fn2()

3、str.concat(str1) === str+str1 拼接字符串

function fn3() {
        let str = '字符串常用的方法'
        str = str.concat('123')
        console.log(str)
      }
      // fn3()

4、replace 替换

function fn4() {
        let str = '字符串常用的123方法123'
        // 把所有的123全部替换
        str = str.replace(/123/g, '***')
        console.log(str)
      }
      // fn4()

5、split(str) 使用指定字符str对字符串进行分割成数组

function fn5() {
        let str = 'font-size-12px'
        let arr = str.split('')
        console.log(arr) // ['font','size','12px']
        let st = ['font', 'size', '12px'].join('-')
        console.log(st)
      }
      // fn5()

6、包含a或A的字符串个数 str.includes('a')

      function fn6() {
        let arr = ['American', 'Greece', 'Canada', 'Russia', 'China']
        let count = 0
        for (let item of arr) {
          // let array = item.split('')
          // console.log(array)
          // if (array.includes('a') || array.includes('A')) {
          //   count++
          // }
          if (item.includes('a') || item.includes('A')) {
            count++
          }
        }
        console.log(count)
      }
      // fn6()

7、indexOf() lastIndexOf()

function fn7() {
        let str = '95243611@qq.com'
        // let index = str.indexOf('#')
        // let lastIndex = str.lastIndexOf('¥')
        // console.log(index, lastIndex)
        // 判断邮箱是否合法
        // 判断@是否有
        let start = str.indexOf('@')
        console.log(start, str.length)
        if (start === -1) {
          console.log('邮箱必须包含@')
          return false
        }
        // 判断是否首位
        if (start === 0) {
          console.log('邮箱@不能在首位')
          return false
        }
        // 判断在末尾
        if (start === str.length - 1) {
          console.log('邮箱@不能在尾部')
          return false
        }
        // 判断是否唯一
        let end = str.lastIndexOf('@')
        if (start !== end) {
          console.log('邮箱只能有一个@')
          return false
        }
        console.log('邮箱正确')
        return true
      }
      // fn7()

8、匹配字符串中的指定字符,返回数组

function fn8() {
        let fileName = '张三20220809.zip'
        // 提取所有汉字
        let arr = fileName.match(/[\u4e00-\u9fa5]/g)
        console.log(arr)
        // 提取所有的数字
        arr = fileName.match(/\d/g)
        console.log(arr)
        // 提取所有的英文
        arr = fileName.match(/[a-zA-Z]/g)
        console.log(arr)
        // 提取所有的.
        arr = fileName.match(/\./g)
        console.log(arr)
        // 已经交作业的学生
        let array = ['张三20220809.zip', '李四20220809.zip', '王五20220809.zip']
        // 班级现有的同学名单
        let arrays = ['张三', '李四', '王五', 'tom']
        let date = array[0].match(/\d/g).join('')
        console.log(date)
        // 交作业的学生名单
        let arraySend = array.map(function (item) {
          return item.match(/[\u4e00-\u9fa5]/g).join('')
        })
        let arrUnsend = []
        for (let item of arrays) {
          if (!arraySend.includes(item)) {
            arrUnsend.push(item)
          }
        }
        console.log(`${date}没交作业的同学有:${arrUnsend.join(',')}`)
      }
      // fn8()

9、转大小写

function fn9() {
        let str = 'border-left-color'
        // 分割成['border','left','color']
        let arr = str.split('-')
        let str1 = ''
        for (let i = 0; i < arr.length; i++) {
          let item = arr[i]
          let arr1 = item.split('')
          // ['b', 'o', 'r', 'd', 'e', 'r']
          // ['l', 'e', 'f', 't']
          // ['c', 'o', 'l', 'o', 'r']
          console.log(arr1)
          if (i !== 0) {
            arr1[0] = arr1[0].toUpperCase()
          }
          str1 += arr1.join('')
        }
        console.log(str1)
      }
      // fn9()

10 、substring(s,e) [s,e)

function fn10() {
        let str = 'img1.png'
        let index = str.indexOf('.')
        // 后缀
        let suffix = str.substring(index + 1)
        console.log(suffix)
        let arr = ['jpg', 'png', 'gif', 'bmp', 'webp', 'jpeg']
        if (arr.includes(suffix)) {
          console.log(str + '是图片')
        } else {
          console.log(str + '不是图片')
        }
      }
      // fn10()

11、str.endswWith(suffix) 判断str字符串是否以suffix结尾

function fn11() {
        let filename = 'img1.png'
        if (filename.endsWith('png')) {
          console.log('文件是图片')
        } else {
          console.log('文件不是图片')
        }
      }
      // fn11()

12、str.startsWith(prefix) 判断str字符串是否以prefix开头

function fn12() {
        let url = 'htt://www.baid.com'
        if (url.startsWith('http')) {
          console.log('路径正确')
        } else {
          console.log('路径不正确')
        }
      }
      // fn12()

13、'Blue Whale'.includes('blue') 包含

function fn13() {
        console.log('blue Whale'.includes('blue')) // false
      }
      // fn13()

14、padEnd(2,'0') padStart(4,'0')

15、slice(s,e) 截取字符串中[s,e)下标范围内的字符

function fn15() {
        let str = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz1234567890'
        let s = str.indexOf('E')
        let e = str.indexOf('K')
        let newStr = str.slice(s, e + 1)
        console.log(newStr)
      }
      // fn15()

16、toString() 返回当前对象的字符串方式

function fn16() {
        console.log(Number(1).toString() + 234)
      }
      // fn16()

17、trim() 首尾去空格 trimStart() trimEnd()

function fn17() {
        let str = ' hello '
        str = '1' + str.trimEnd() + '1'
        console.log(str)
      }
      fn17()

18.String对象的常用方法和属性:

    1、length

    2、charAt(index)

    3、concat()

    4、replace(oldStr,newStr)

    5、split()

    6、includes()

    7、indexOf()

    8、lastIndexOf()

    9、match()

    10、toUpperCase()

    11、toLowerCase()

    12、substring()

    13、endsWith()

    14、startsWith()

    15、padEnd()

    16、padStart()

    17、toString()

    18、trim()

    19、trimStart()

    20、trimEnd()

3.Math的常用api

const PI = Math.PI
let str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'
// str[i]

1、ceil()

function fn() {
  let num = 3.14
  Math.ceil(num) // 4
  num = -3.14
  Math.ceil(num) // -3
  let total = 107
  let pagesize = 7
  // ?  totalPage
  let totalPage = Math.ceil(total / pagesize)
  console.log(totalPage, total % pagesize)
}
// fn()

2、floor()

function fn2() {
  let num = 3.14
  Math.floor(num) // 3
  num = -3.14
  Math.floor(num) // -4
}
// fn2()

3、round() 四舍五入取整,不分符号

function fn3() {
  let num = -3.54
  console.log(Math.round(num))
}

4、random() [0,1)之间的数字

function fn4(min, max) {
  // for (let i = 0; i < 100; i++) {
  //   let num = parseInt(Math.random() * (max - min + 1)) + min
  //   console.log(num)
  // }
  // 获得指定范围在随机数字 [min,max]
  /*
    min<= parseInt(Math.random()*(max-min+1))+min <=max
  */
  for (let i = 0; i < 100; i++) {
    let index = parseInt(Math.random() * (max - min + 1)) + min
    console.log(str[index])
  }
}

// fn4(0, str.length - 1)
// 随机产生10个字符串,字符串长度6-12位s
function fn4_1() {
  let arr = []
  for (let i = 0; i < 10; i++) {
    let st = ''
    let len = parseInt(Math.random() * (12 - 6 + 1)) + 6
    for (let j = 0; j < len; j++) {
      let index = Math.floor(Math.random() * str.length)
      st += str[index]
    }
    arr.push(st)
  }
  console.log(arr)
}
fn4_1()
/*
  0<= Math.floor(Math.random()*str.length) <=61
*/
let arr = [1, 2, 4, 5, 2]
let element = arr[Math.floor(Math.random() * arr.length)]
console.log(element)

5、abs()

Math.abs(-3.14) // 3.14

6、max(1,3,2,5)

7、min(1,3,2,5)

8、Math.sqrt(2) // 1.414

9、pow(x,y) x的y次方,y可以是小数

console.log(Math.pow(2, 0.5))

10、cbrt()

console.log(Math.cbrt(27))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值