字符串(string)
一、总汇
1 | charAt() 获取相应位置的字符 |
二、增
concat():可以传任何个数的参数,用于字符串的拼接,返回连接后的字符串,而原字符串不受影响
1
2
3var str1 = '你好',str2 = 'hello';
str1 = str1.concat(str2); // 你好hello
console.log(str1.concat(' world')); // 你好hello world+号
1
2var str1 = 'hello', str2 = 'world';
console.log(str1 + ' ' + str2); // helloworld
三、删
slice(start,end):截取字符串 (返回新的字符串,原字符串不受影响)
1
2
3
4
5
6
7截取从start位置开始,end位置结束(不包含 end) 的字符串,返回一个新字符串 若参数为负数,需要从尾部位置倒数 负数参数长度 作为生效位置start大于end则返回空字符串start大于字符串长度,返回空字符串
var str = 'hello world';
// slice(star) 一个参数表示从star位置截取到后面所有的字符
var str1 = str.slice(6), // world
str2 = str.slice(0, 5), // hello
str3 = str.slice(5, 1), // null
str4 = str.slice(20); // nullsubstr(start, length):从指定位置截取固定长度的字符串 (返回新的字符串,原字符串不受影响)
1
2
3
4
5
6截取从 start 位置开始, 截取长度为 length ,若 length 为负数,会被重置成 0,将返回空字符串start 大于字符串长度,返回空字符串
var str = 'hello world';
var str1 = str.substr(6), // world
str2 = str.substr(0, 5), // hello
str3 = str.substr(3, 5), // lo wo
str4 = str.substr(20); // null
四、改
toUpperCase() 字符串转大写 toLowerCase() 字符串转小写
1
2
3var str = 'hello嘻嘻嘻world',
str1 = str.toUpperCase(), // HELLO嘻嘻嘻WORLD
str2 = str1.toLowerCase(); // hello嘻嘻嘻world
五、查
charAt() 获取相应位置的字符 charCodeAt() 指定位置字符 的 Unicode 编码
1
2
3var str = "how are you? and you?";
console.log(str.charAt(5)); // r
console.log(str.charCodeAt(5)) // 114indexOf() 从字符串开头查找返回字符在字符串中的位置 lastIndexOf() 从字符串结尾查找返回字符在字符串中的位置
1
2
3
4
5var str = "how are you? and you?";
console.log(str.indexOf("a")); // 4
console.log(str.lastIndexOf("y")); // 17
// 如果没有找到的字符则返回 -1
console.log(str.indexOf("z"),str.lastIndexOf("z")); // -1 -1
六、其他方法
trim() 去除字符串两侧空格
1
2
3
4
5var str = ' an dy ';
console.log(str.trim()); // an dy
var str1 = ' ',
str2 = str1.trim();
console.log(Boolean(str1),Boolean(str2)); // true false
数组(Array)
一、总汇
1 | push() 在末端添加一个元素。 |
二、增
arr.push(data) 在数组后添加数据,返回添加后的数组长度 (会改变原数组)
1
2
3var arr = [1, 5, '笑笑', '6', 8];
console.log(arr.push(68)); // 6
console.log(arr); // [1, 5, "笑笑", "6", 8, 68]arr.unshift(data)在数组前添加数据,返回添加后的数组长度 (会改变原数组)
1
2
3var arr = [1, 5, '笑笑', '6', 8];
console.log(arr.unshift(68)); // 6
console.log(arr); // [68, 1, 5, "笑笑", "6", 8]
三、删
arr.pop() 在数组后删除数据,并返回删除的数据 (会改变原数组)
1
2
3var arr = [1, 5, '笑笑', '6', 8];
console.log(arr.pop()); // 8
console.log(arr); // [1, 5, "笑笑", "6"]arr.shift() 在数组前删除数据,并返回删除的数据 (会改变原数组)
1
2
3var arr = [1, 5, '笑笑', '6', 8];
console.log(arr.shift()); // 1
console.log(arr); // [5, "笑笑", "6", 8]
四、改
arr.slice(start, end) 从start开始,到end结束[start, end)的所有内容。(不会改变原数组)
1
2
3
4
5
6返回从原数组中指定开始下标到结束下标之间的项组成的新数组。slice()方法可以接受一或两个参数,即要返回项的起始和结束位置。在只有一个参数的情况下, slice()方法返回从该参数指定位置开始到当前数组末尾的所有项。如果有两个参数,该方法返回起始和结束位置之间的项——但不包括结束位置的项。
var arr = [1, 5, '笑笑', '6', 8];
console.log(arr.slice(0,3)); // [1, 5, "笑笑"]
console.log(arr.slice(3)); // ["6", 8]
console.log(arr.slice(2,3)); // ["笑笑"]
console.log(arr.slice(2,1)); // []arr.splice(start, end, 修改) [start, end) (会改变原数组)
1
2
3
41个参数:从这个参数的下标截取到最后一个值,存储为一个新的数组返回
var arr = [1, 5, '笑笑', '6', 8];
console.log(arr.splice(2)); // ["笑笑", "6", 8]
console.log(arr); // [1, 5]1
2
3
42个参数:第一个参数为起始下标,第二个参数为截取个数
var arr = [1, 5, '笑笑', '6', 8];
console.log(arr.splice(0,2)); // [1, 5]
console.log(arr); // ["笑笑", "6", 8]1
2
3
4
5
6
7
8
9
10
11
12
133个参数: 插入数据
var arr = [1, 5, '笑笑', '6', 8];
console.log(arr.splice(3,0,99)); // []
console.log(arr); // [1, 5, "笑笑", 99, "6", 8]
3个参数: 修改数据
var arr = [1, 5, '笑笑', '6', 8];
console.log(arr.splice(3,1,99)); // ["6"]
console.log(arr); // [1, 5, "笑笑", 99, 8]
var arr1 = [1, 5, '笑笑', '6', 8];
console.log(arr1.splice(3,2,99,96,55)); // ["6", 8]
console.log(arr1); // [1, 5, "笑笑", 99, 96, 55]
五、查
arr.includes(data) 查找内容是否存在,存在返回true,反之返回false
arr.indexOf(data) 从前往后查找内容是否存在,存在返回所在的下标,反之返回-1
arr.lastIndexOf(data) 从后往前查找内容是否存在,存在返回所在的下标,反之返回-1