String对象有哪些方法?
String对象的属性有:
(1) length (返回字符串的字符长度)
(2) prototype
(3) constructor
String对象的方法:
charAt(); charCodeAt(); indexOf(); match(); replace(); search(); slice(); toUpperCase(); toLowerCase(); 等方法
- (1)charAt() —-返回字符串在下标index位置的字符
stringObject.charAt(index) // 如果参数index不在0与字符串长度之间,则返回一个空的字符串
'kdfjrjrtj'.charAt(3); // ====>返回 'j';
- (2) charCodeAt() —- 返回字符串相应下标index位置的字符的unicode编码
stringObject.charCodeAt(index) // 返回unicode编码
'sjdlfkjsrf'.charCodeAt(3); // 108 ('l'的unicode编码为108)
- (3) indexOf() —-返回某个指定的字符串值在字符串中首次出现的位置
stringObject.indexOf(searchvalue,fromindex);
// searchvalue为需要搜索的子字符串,fromindex为从某个下标开始查询
'hello world!'.indexOf('lo wo'); // 3 (子字符串首次出现的位置)
- (4) match() —- 在字符串内检索指定的值,或找到一个或多个正则表达式的匹配
stringObject.match(regexp) // regexp为匹配子字符串的正则表达式
var str = 'hello world , l love the world';
str.match(/world/g); // [world, world]
- (5) replace()
var str = 'hello world , l love the world';
str.replace(/world/g,'java'); // "hello java , l love the java"
// 其中str并没有被改变
(6)search()
(7) slice() —提取字符串的某个部分,并以新的字符串返回被提取的部分
stringObject.slice(start, end)
var str = "Hello Microsoft!";
console.log(str.slice(6)); //Microsoft!
console.log(str.slice(6, 12)); //Micros
- (8) toUpperCase()和toLowerCase()
Array对象的常用属性和方法
Array对象的属性有:
(1) length (返回数组的长度)
(2) prototype
(3) constructor
Array对象的方法:
shift(); unshift(); pop(); push(); concat(); reverse(); splice(); slice(); 等方法
- (1)shift(); —删除数组的第一个元素
var arr = ['the','world','is','beautiful'];
arr.shift(); // 'the'
console.log(arr); // ['world','is','beautiful']
// 会改变原来的数组
- (2) unshift(); —向数组开头添加元素; 返回的是新数组的长度
var arr = ['the','world','is','beautiful'];
arr.unshift('dkfj') // 5 表示新数组的长度为5
console.log(arr); // ["dkfj", "the", "world", "is", "beautiful"]
(3) pop(); —-删除数组的最后一个元素
(4) push(); —向数组最后添加元素
(5) concat() — 连接多个数组
(6) reverse(); — 反转数组
(7) splice(); slice()