JavaScript——ES5和字符串

一、ES5
  1. 严格模式:让js代码在更严格的条件下运行

  1. 目的:

-消除JavaScript语法的一些不合理、不严谨之处,减少一些怪异行为;

-消除代码运行的一些不安全之处,保证代码运行的安全;

-提高编译器效率,增加运行速度;

-为未来新版本的JavaScript做好铺垫。

进入"严格模式"的标志:"use strict";

  1. 作用:

(1) 不允许隐式声明变量

(2)禁止函数中的形参重名

(3)禁止函数中的this代表window

二、字符串
  1. 定义

var str = '';
var str = "";
//注意:在使用字面量创建字符串时,需要注意创建的内容中是否也有单双引号;
/*
例如:‘<div style='color:red'>’大家好<div>'  //运行报错
     ‘<div style=”color:red“>’大家好<div>'  //正常
*/
  • 字符串长度:字符串.length

  • 字符串下标:字符串[下标]

  • 字符串遍历

var str = "abcdef";
for(var i=0;i<str.length;i++){
    console.log(str[i],i)
}
  • 字符串不能进行增删改查,可以进行比较

字符串比较的规则是逐字符进行比较,大小由阿斯克码(ascii)来决定。

  1. 字符串方法

注意:字符串中的方法,不会对源字符串有变化

(1)charAt(下标):根据下标获取该位置上所对应的字符;

var str = 'abcdef';
var res=str.charAt(4)
console.log(res) //e

(2)charCodeAt(下标):获取指定位置上字符的ascii

var str = 'abcdef';
var res=str.charCodeAt(1)
console.log(res)   //98

(3)indexOf(字符,下标):从左往右,从指定下标开始查找当前字符在整个字符串中是否存在,如果存在,则返回第一次出现的位置,若不存在,就会返回-1

var s1='hello world'
var ind1=s1.indexOf('l')
var ind2=s1.indexOf('ll',2)
console.log(ind1,ind2)  //2 2

(4)LastIndexOf(字符,下标):从右往左,从指定下标开始查找当前字符在整个字符串中是否存在,如果存在,则返回最后一次出现的位置;如果存在,则返回第一次

var s1='hello world'
var ind3=s1.lastIndexOf('ll')
var ind4=s1.lastIndexOf('l',7)
console.log(ind3,ind4)  // 2 3

(5)slice(index1,index2):从index1开始截取到index2位置上字符串,包含index1,不包含index2;下标可以为负数

var s1='hello world'
var s2=s1.slice(1,8)
console.log(s2)    //ello wo

(6)subString(index1,index2):从index1开始截取到index2位置上字符串,包含index1,不包含index2;下标只能是正数,不可以为负数

var s1='hello world'
var s3=s1.substring(1,4)
console.log(s3)  //ell

(7)substr(index1,个数):从index1开始,截取指定长度的字符串;

var s1='hello world'
var s4=s1.substr(1,2)
console.log(s4)   //el

(8)replace(旧字符,新字符):使用新字符串去替换第一个满足条件的旧字符串,返回替换之后的字符串

var s2="hello 你好啊,hello 大家好"
var str1=s2.replace('hello','丽丽')
console.log(str1)   //丽丽 你好啊,hello 大家好

(9)replaceAll(旧字符,新字符):使用新字符串去替换所有满足条件的旧字符串,返回替换之后的字符串

var s2="hello 你好啊,hello 大家好"
var str1=s2.replaceAll('hello','丽丽')
console.log(str1)   //丽丽 你好啊,丽丽 大家好

(10)concat(字符串,字符串):合并多个字符串

var s2="hello 你好啊,hello 大家好"
var str2=s2.concat("才是真的好")
console.log(str2)    //hello 你好啊,hello 大家好才是真的好

(11)trim():去除字符串左右两边的空格

var s2=" hello 你好啊,hello 大家好 "
var str3=s2.trim()
console.log(str3) //hello 你好啊,hello 大家好
console.log(s2)  // hello 你好啊,hello 大家好 

(12)split(分割符):根据指定的分割符来分割字符串,并转为数组

var s2="hello 你好啊,hello 大家好"
var arr1=s2.split('o')
console.log(arr1)  //['hell',' 你好啊,hell',' 大家好']

(13) toLowerCase():把所有的字符转为小写

var s1="HELLO WORLD"
var str=s1.toLowerCase()
console.log(str)   //hello world

(14)toUpperCase():把所有的字符转为大写

var s2="hello world"
var str6=s2.toUpperCase()
console.log(str6)  //HELLO WORLD

(15)includes('字符'):判断字符串中是否包含某个字符

var str='abcdef'
var bool1=str.endsWith('cd')  // true
var bool2=str.endsWith('f')   // true
var bool3=str.endsWith('acd') // false

(16)startsWith('字符'):判断字符串是否以某个字符或小字符串开头

var str = 'abcdef'
var bool1 = str.startsWith('a') // true
var bool2 = str.startsWith('ab') // true
var bool3 = str.startsWith('aa') // false

(17)endsWith('字符'):判断字符串是否以某个字符或小字符串结尾

var str = 'abcdef'
var bool1 = str.endsWith ('f') // true
var bool2 = str.endsWith ('ef') // true
var bool3 = str.endsWith ('ff') // false

(18)String.fromCharCode(ASCII):根据指定的阿斯克码得到对应的字符

// 获取98对应的字符
var res = String.fromCharCode(98); // 参数为指定的阿斯克码
console.log(res); // b

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值