String对象

1.String对象属性

(1) length:字符串的长度
用法:

	var a='abc'
	console.log(a.length)   // 3

2.String对象的方法

(1) charAt():返回指定位置的字符

    var a='abc'
 	console.log(a.charAt(2))  // c

(2) indexOf():返回指定的字符串首次出现的位置

	var a='abcdeabcde'
    console.log(a.indexOf('b'))   // 1

(3) concat():连接两个或多个字符串

	var a='123'
    var b='456'
    var c='789'
    console.log(a.concat(b))  // 1234546
    console.log(a.concat(b.concat(c)))  //123456789

(4) split(a,b):把字符串分割成字符串数组
a为字符串或正则表达式,b为数组的最大长度(数字)
当a是“”时,则每个字符之间都会被分割

	 var a='a b c d e f'
    document.write(a.split(' ',2)+"<br>")  // a,b
    document.write(a.split(''))   // a, ,b, ,c, ,d, ,e, ,f     连空格都分割

(5) slice(a,b) :截取字符串的片段
a为起始下标,b为结束下标 [a,b)

	var a='abcdefg'
    console.log(a.slice(2,6))    // cdef

(6) substring(a,b)截取字符串
a为开始下标,b为结束下标 [a,b)

	var a='abcdefg'
    console.log(a.slice(2,6))    // cdef

(7) substr(a,b)截取字符串
a为起始下标,b为截取的长度

	var a='abcdefg'
    console.log(a.substr(2,2))    // cd

(8) toLowerCase() 转小写
(9) toUpperCase() 转大写

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值