js中String对象和Array对象的常用方法和属性

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()

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值