字符串扩展及数值扩展
故心故心故心故心小故冲啊
一、字符串扩展
es5
var str = 'hello wrold';
//属性 截取字符串的长度
console.log(str.length); //11
//方法
//charAt() 方法可返回指定位置的字符
console.log(str.charAt(2)); //l
console.log(str.charAt(str.length-1)); //d 获取最后一个字符
//concat() 方法用于连接两个或多个字符串
var str1 = 'hello ';
var str2 = 'gx';
var s = str1.concat(str2,' welcome');
console.log(s); //hello gx welcome
//indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。区分大小写
console.log(str.indexOf('o')); //4 匹配成功后返回索引值
console.log(str.indexOf('a')); //-1 没有匹配成功则返回-1
console.log(str.indexOf('o',5)); //8 indexOf(查找的值,开始的位置)
//lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置
console.log(str.lastIndexOf('o')); //8
console.log(str.lastIndexOf('o',5)); //4 后面这个参数是结束的位置
//replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串
var str2 = 'hello gx';
replace(searchValue,newValue) //返回的是新的字符串
var s = str2.replace('hello','hi,');
console.log(s); //hi, gx
//split() 方法用于把一个字符串分割成字符串数组
var str3 = 'how,are,you';
console.log(str3.split(",")); // ["how", "are", "you"]
console.log(str3.split(",",2)); // ["how", "are"] 2表示返回数组的最大长度
//substr() 方法可在字符串中抽取从开始下标开始的指定数目的字符
console.log(str.substr(4)); //o wrold hello wrold
console.log(str.substr(2,4)); //substr(start,length) llo
//substring() 方法用于提取字符串中介于两个指定下标之间的字符
console.log(str.substring(4)); //o wrold
console.log(str.substring(2,4)); //substr(from,to) ll
//slice(start, end) 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分 负数表示倒数
console.log(str.slice(2,4)); //ll
console.log(str.slice(-1)); //d -1表示最后一个字符串
console.log(str.substring(-1)); //-1 表示0
// slice()和substring()区别
console.log(str.slice(3,-4)); //lo w
console.log(str.substring(3,-4)); //hel
//toLowerCase() 方法用于把字符串转换为小写
var str4 ='HELLO'
console.log(str4.toLowerCase());
// //toUpperCase() 方法用于把字符串转换为大写
var str4 ='hello'
console.log(str4.toUpperCase());
//trim() 方法用于删除字符串的头尾空格
var str5 = ' hello ';
console.log(str5.trim()); // hello
es6
//includes(): 返回布尔值,表示是否找到了参数字符串。
var str = 'hello world' ;
str.includes('h');
if(str.indexOf('h') !=-1);
//模糊查询
var arr = ['abc','xyz','red','blue','orange','green','ok']; //查询的列表数据
var input = 'a'; //输入的条件
var select = arr.filter(function(item){
return item.includes(input);
//return item.indexOf(input) !=-1; //==-1不匹配 !=-1匹配
});
console.log(select)
//startsWith(): 返回布尔值,表示参数字符串是否在原字符串的头部。
var str = 'hello world' ;
str.startsWith('h') //true
str.startsWith('h',4) //false //第二个参数表示搜索的位置
str.startsWith('H') //false 区分大小写
//endsWith(): 返回布尔值,表示参数字符串是否在原字符串的尾部
//repeat(): 返回一个新字符串,表示将原字符串重复n次。
var str = 'hello' ;
str.repeat(2) //hellohello
//padStart(): 用于头部补全
var str = 'ok' ;
str.padStart(5,'abc') //abcok 5表示补全的长度,'abc'补全的内容
//padEnd(): 用于尾部补全。
二、数值扩展
es5
//parseInt将字符串转化为数字
var s = '20';
console.log(parseInt(s));
console.log(parseInt("10 20 30")); //10
console.log(parseInt("10 year")); //10
console.log(parseInt("year 10")); //NaN
//Number js变量转化为数值 (false null [] 返回为0)
console.log(Number(true)); //1
console.log(Number(false)); //0
console.log(Number(null)); //0
console.log(Number(undefined)); //NaN
console.log(Number("abc")); //NaN
console.log(Number({}));//NaN
console.log(Number([]));//0
console.log(Number(function(){}));//NaN
//toFixed(小数点的位数) 四舍五入
var n = 23.95
var n1 = 23.55
console.log(n.toFixed());//24
console.log(n1.toFixed());//23
console.log(n1.toFixed(1));//23.6
console.log(n1.toFixed(2));//23.55
es6
//ES5
// parseInt() 函数可解析一个字符串,并返回一个整数。
parseInt('11.111'); //11
//ES6
Number.parseInt('11.11') //11
// parseFloat() 函数可解析一个字符串,并返回一个浮点数
parseFloat('11.111'); //11.111
// Number.isInteger()用来判断一个数值是否为整数。返回true false
//JavaScript 内部,整数和浮点数采用的是同样的储存方法,所以 11 和 11.0 被视为同一个值。
Number.isInteger(11) //true
Number.isInteger(11.0) //true
Number.isInteger(11.11) //false
//如果参数不是数值,Number.isInteger返回false。
Number.isInteger() // false
Number.isInteger(null) // false
Number.isInteger('15') // false
Number.isInteger(true) // false
// Math.ceil() 返回大于或等于一个给定数字的最小整数
Math.ceil(11.11) //12 上舍入
// Math.floor() 返回小于或等于一个给定数字的最大整数
Math.floor(11.999) //11 下舍入
// Math.round() 返回一个数字四舍五入后最接近的整数
Math.round(11.6) //12 四舍五入
// Math.trunc() 用于去除一个数的小数部分,返回整数部分。
Math.trunc(4.1) //4
Math.trunc(-4.9) //-4
//对于非数值,Math.trunc内部使用Number方法将其先转为数值。
Math.trunc('123.456') // 123
Math.trunc(true) //1
Math.trunc(false) // 0
Math.trunc(null) // 0
//对于空值和无法截取整数的值,返回NaN。
Math.trunc(NaN); // NaN
Math.trunc('foo'); // NaN
Math.trunc(); // NaN
Math.trunc(undefined) // NaN
// Math.sign() 方法用来判断一个数到底是正数、负数、还是零。对于非数值,会先将其转换为数值。
//五种值
//正数,返回+1
//负数,返回-1
//0 返回0
//-0 返回-0
//其它值 返回NaN
Math.sign(6) //1
Math.sign(-6) //-1
Math.sign(0) //0
Math.sign('abc') //NaN
//如果参数是非数值,会自动转为数值。对于那些无法转为数值的值,会返回NaN。
Math.sign('') // 0
Math.sign(true) // +1
Math.sign(false) // 0
Math.sign(null) // 0
Math.sign('9') // +1
Math.sign('foo') // NaN
Math.sign() // NaN
Math.sign(undefined) // NaN
//指数运算符(**)
2 ** 2 // 4
2 ** 3 // 8
//这个运算符的一个特点是右结合,而不是常见的左结合。多个指数运算符连用时,是从最右边开始计算的。
// 相当于 2 ** (3 ** 2)
2 ** 3 ** 2 // 512
//指数运算符可以与等号结合,形成一个新的赋值运算符(**=)。
a **= 2; // 等同于 a = a * a;
b **= 3; // 等同于 b = b * b * b;