js字符串
js常用的字符串
trim 去空格
trim去除两端字符串空白
trim() 方法返回一个从两头去掉空白字符的字符串,并不影响原字符串本身。
const str = " afv Hello word "
console.log(str.trim())
afv Hello word
toUpperCase 字符串字母大写
方法将调用该方法的字符串转为大写形式并返回(如果调用该方法的值不是字符串类型会被强制转换)。
toUpperCase() 返回转为大写形式的字符串。此方法不会影响原字符串本身的值,因为JavaScript中字符串的值是不可改变的。
const str = "afv Hello word"
console.log(str.toUpperCase());
AFV HELLO WORD
toLowerCase 字符串字母小写
会将调用该方法的字符串值转为小写形式,并返回。
const str = "afv Hello word"
console.log(str.toLowerCase());
afv hello word
concat 字符串拼接
concat() 方法将字符串参数连接到调用的字符串,并返回一个新的字符串。
const str = "afv Hello word"
console.log(str.concat("xiao"));
afv Hello wordxiao
replace 替换
replace() 方法返回一个由替换值(replacement)替换部分或所有的模式(pattern)匹配项后的新字符串。模式可以是一个字符串或者一个正则表达式,替换值可以是一个字符串或者一个每次匹配都要调用的回调函数。如果pattern是字符串,则仅替换第一个匹配项。原字符串不会改变。
const str = "afv Hello word"
console.log(str.replace('word', 'dog'))
afv Hello dog
slice 提取字符串
slice() 方法提取字符串的一部分,并将其作为新字符串返回,而不修改原始字符串。
const str = "afv Hello word"
console.log(str.slice(2, 9))
v Hello
includes 判断包含字符串
includes() 方法用于判断一个字符串是否包含在另一个字符串中,根据情况返回 true 或 false。
只能判断连续的字符串 不能判断间隔字符串的内容
includes() 方法是区分大小写的
如果是两个参数 第二个参数可以指出被搜寻字符串的索引
const str = "afv Hello word"
console.log(str.includes('q')) // false
console.log(str.includes('word',3)) //ture
split 分割
split() 方法接受一个模式,通过搜索模式将字符串分割成一个有序的子串列表,将这些子串放入一个数组,并返回该数组。
const str = "afv Hello word"
console.log(str.split(""))
(14) ['a', 'f', 'v', ' ', 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'd']
indexOf
String 的 indexOf() 方法在字符串中搜索指定子字符串,并返回其第一次出现的位置索引。它可以接受一个可选的参数指定搜索的起始位置,如果找到了指定的子字符串,则返回的位置索引大于或等于指定的数字。
const str = "afv Hello word"
console.log(str.indexOf('e')) //5
console.log(str.indexOf('d'))//13
console.log(str.indexOf('a'))//0
toFixed
toFixed() 方法使用定点表示法来格式化一个数值。数值类型
let numb = 13546.556
console.log(numb.toFixed(2)) //13546.56
console.log(numb.toFixed())//13547
四舍五入
let numb = 1.55
let numb2 = 1.56
let numb3 = 1.54
console.log(numb.toFixed(1)); //1.6
console.log(numb2.toFixed(1)); //1.6
console.log(numb3.toFixed(1));//1.5
四舍六入
let numb = 1.55
let numb2 = 2.55
let numb3 = 2.56
let numb4 = 3.55
let numb5 = 3.56
console.log(numb.toFixed(1)); //1.6
console.log(numb2.toFixed(1)); //2.5
console.log(numb3.toFixed(1)); //2.6
console.log(numb4.toFixed(1)); //3.5
console.log(numb5.toFixed(1)); //3.6
startsWith
startsWith() 方法用来判断当前字符串是否以另外一个给定的子字符串开头,并根据判断结果返回 true 或 false。
语法:str.endsWith(searchString[, length]) searchString:要搜索的子字符串。 length:作为 str 的长度。默认值为 str.length。
let str = '风吹神像背,庙透了'
console.log(str.startsWith('风')); //true
console.log(str.startsWith('吹', 1));//true
endswith
endsWith()方法用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果返回 true 或 false。
语法: str.startsWith(searchString[, position])
searchString:要搜索的子字符串。
position :在 str 中搜索 searchString 的开始位置,默认值为 0。
let str = '风吹神像背,庙透了'
console.log(str.endsWith('了')); //true
console.log(str.endsWith('庙透了'));//true
解构
let obj = {
name: 'pink',
age: '25',
move: 'cleck'
}
console.log(obj);
// { name: 'pink', age: '25', move: 'cleck' }
const { name, age, move } = obj
console.log(name, age, move);
// pink 25 cleck
let arr = [4, 5, 6, 8, 'pink']
const [a, b, c, d, e] = arr
console.log(a, b, c, d, e);
// 4 5 6 8 'pink'
展开运算
let a = [4, 1, 6, 8, 9]
let b = ['pink', 'blue', 'se']
let c = [...a,...b]
console.log(c)
// 4 1 6 8 9 'pink' 'blue' 'se'
定时器
定时器 定时多久去执行 格式: setInterval(()=>{},时间(毫秒))
const timerId = setInterval(() => {
console.log(123);
}, 500);
console.log(timerId);
//清除定时器
setTimeout(()=>{
clearInterval(timerId)
},5000)
延时器
延时器 延时多少去执行 格式 :seTtimeout(()=>{},时间(毫秒))
const fn = ()=>{
console.log('我是函数,我要执行了');
}
setTimeout(()=>{
console.log('我是延时器,我执行了');
fn()
},3000)
for in 遍历对象
let obj = {
name: '张三',
age: 25,
job: 'CEO'
}
for (const key in obj) {
console.log(obj[key])
}
//张三 25 CEO
typeof 查看数据类型
console.log(typeof 1); // number
console.log(typeof "张三"); //string
console.log(typeof []); // object
console.log(typeof {}); // object
console.log(typeof undefined); // undefined
console.log(typeof null); // object
const fn = () => {};
console.log(typeof fn); // function